943,777 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 8335
  • PHP RSS
Jan 19th, 2008
0

date range php/mysql

Expand Post »
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.
PHP Syntax (Toggle Plain Text)
  1. $strSQL = 'SELECT * '
  2. . ' FROM `calendar` '
  3. . ' WHERE `eventdate` '
  4. . ' BETWEEN "2008-04-30" AND "2008-06-23"'
  5. . ' 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)
  1. <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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
thecraigmcrae is offline Offline
1 posts
since Jan 2008
Jan 19th, 2008
0

Re: date range php/mysql

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 should be able to do this with some php.

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)
  1. <?php
  2. $test_date = strtotime("21 June 2008");
  3.  
  4. $quarters = array();
  5.  
  6. $season['start'] = strtotime("1 January 2008");
  7. $season['end'] = strtotime("1 April 2008");
  8. $season['name'] = 'First';
  9.  
  10. $quarters[] = $season;
  11.  
  12. $season['start'] = strtotime("1 April 2008");
  13. $season['end'] = strtotime("1 July 2008");
  14. $season['name'] = 'Second';
  15.  
  16. $quarters[] = $season;
  17.  
  18. $active_quarter = '';
  19. foreach ($quarters as $one)
  20. {
  21. if (($one['start'] < $test_date) && ($test_date < $one['end']))
  22. $active_quarter = $one['name'];
  23. }
  24.  
  25. if ($active_quarter != '')
  26. echo "Match: $active_quarter";
  27. else
  28. echo "No match!";
  29. ?>
Reputation Points: 29
Solved Threads: 5
Junior Poster in Training
Walkere is offline Offline
57 posts
since Jan 2008
Jan 20th, 2008
0

Re: date range php/mysql

I was bored, so I wrote up a simple php class to do this type of thing. You can get the source code and some documentation here.
Reputation Points: 29
Solved Threads: 5
Junior Poster in Training
Walkere is offline Offline
57 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Php Sessions
Next Thread in PHP Forum Timeline: Web and widget developers needed





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC