| | |
date range php/mysql
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jan 2008
Posts: 1
Reputation:
Solved Threads: 0
I'd been trying to do date range comparisons all day, and found that using sql was the only way to find out if a date (inside a table) was between a range (assigned by me)
I have 4 of the statements below, one for each season.
The problem i'm having now, is that i need to assign a season to each date range to use in my document as a variable (
Any help would be appreciated, i'm tearing my hair out over this.
I have 4 of the statements below, one for each season.
PHP Syntax (Toggle Plain Text)
$strSQL = 'SELECT * ' . ' FROM `calendar` ' . ' WHERE `eventdate` ' . ' BETWEEN "2008-04-30" AND "2008-06-23"' . ' ORDER BY `eventdate` ASC';
The problem i'm having now, is that i need to assign a season to each date range to use in my document as a variable (
$quarter). What i figured i'd need to do was see if the current date was within that range, so i'd need to get the system date. Not sure how to achieve this in sql and pass the variable through to use in the document. PHP Syntax (Toggle Plain Text)
<a href="" onclick="return showPanel(this, 'fall');" <?php if($quarter == "fall"){ echo("class='active'"); }?> >FALL</a>
Any help would be appreciated, i'm tearing my hair out over this.
Last edited by thecraigmcrae; Jan 19th, 2008 at 8:55 pm.
•
•
Join Date: Jan 2008
Posts: 57
Reputation:
Solved Threads: 5
•
•
•
•
I'd been trying to do date range comparisons all day, and found that using sql was the only way to find out if a date (inside a table) was between a range (assigned by me)
You can use the strotime() function to turn the date from the table into a timestamp. Then use strotime to turn your start and end dates into a timestamp. Then do a simple if statement comparison - if the start date is less than the table date and the table date is less than the end date... you're good.
You could create an array, with four elements. Each one of those elements would be its own array - with a start date, end date, and quarter name.
Then loop through the entire array, do the if comparison, and when you have a match save the quarter name.
Here's a working script to get you started. You'd just need to punch in your data from the db table and add the correct date ranges in.
PHP Syntax (Toggle Plain Text)
<?php $test_date = strtotime("21 June 2008"); $quarters = array(); $season['start'] = strtotime("1 January 2008"); $season['end'] = strtotime("1 April 2008"); $season['name'] = 'First'; $quarters[] = $season; $season['start'] = strtotime("1 April 2008"); $season['end'] = strtotime("1 July 2008"); $season['name'] = 'Second'; $quarters[] = $season; $active_quarter = ''; foreach ($quarters as $one) { if (($one['start'] < $test_date) && ($test_date < $one['end'])) $active_quarter = $one['name']; } if ($active_quarter != '') echo "Match: $active_quarter"; else echo "No match!"; ?>
![]() |
Similar Threads
- When a host says "included scripts" (Networking Hardware Configuration)
Other Threads in the PHP Forum
- Previous Thread: Help. Parse error in PHP code
- Next Thread: Web and widget developers needed
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code codingproblem cron curl database date directory display download dynamic echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla limit link login loop mail memmory menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search select send server sessions sms snippet soap source space speed sql static structure syntax system table tutorial up-to-date update upload url validation validator variable video web wordpress xml youtube





