Monday, January 8. 2007Creating a Date Range Array with PHPTrackbacks
Trackback specific URI for this entry
No Trackbacks
Comments
Display comments as
(Linear | Threaded)
First of all, thanks for posting this. It put me on the right road to solve a problem I was having with a calendar app.
However, I have a suggestion: instead of using $iDateFrom+=86400 to add 24 hours, use strtotime("+1 day",$iDateFrom) The problem is with daylight saving time. I was having issues with dates at the end of October and after several hours of hair-pulling, found this post: http://us2.php.net/manual/en/function.mktime.php#47079. Hopfully this will keep you and others from wasting time trying to figure out why some dates/date ranges work as expected and some don't
Thanks for the tip. I did some testing of my code over date ranges that included daylight-savings-time changes, and as long as all you want is the YYYY-MM-DD formatted date, the code is OK as it is. However, the time portion of the date can fluctuate from 00:00:00 to 01:00:00 to 02:00:00, so if you need the time to be steady, you might try the approach noted by xentrino, or you could simply append " 00:00:00" to the date string.
I was looking exactly for this. I just googled it and found this article.
I am actually extracting a result set from MySQL that I use to create a graph, and I needed to also show dates with empty values (those for which there is actually no data in the table). I just had to make the dates be the keys of the array, with a value of zero. Works fine for me, thanks for posting your code!
I've just started learning PHP, could you guys give an example how to use this function? Thanks.
You could use it like this to get all the days from February 15 to March 15:
$strDateFrom='2007-02-15'; $strDateTo='2007-03-15'; $aryDates=createDateRangeArray($strDateFrom,$strDateTo); print_r($aryDates);
I just cleaned this up so it was a bit more readable, and figured I would share:
function createDateRangeArray($start, $end) { // Modified by JJ Geewax $range = array(); if (is_string($start) === true) $start = strtotime($start); if (is_string($end) === true ) $end = strtotime($end); if ($start > $end) return createDateRangeArray($end, $start); do { $range[] = date('Y-m-d', $start); $start = strtotime("+ 1 day", $start); } while($start < $end); return $range; }
Brilliant. Everything I attempted to generate an array of dates between a start and end date did not work. This worked like a charm. Nice work.
Use this if you want to include the end date in the range. Line 9 was removed since it's not needed (only needed if you plan to use a smaller end date than start date).
function dateRangeArray($start, $end) { $range = array(); if (is_string($start) === true) $start = strtotime($start); if (is_string($end) === true ) $end = strtotime($end); do { $range[] = date('Y-m-d', $start); $start = strtotime("+ 1 day", $start); } while($start (less than symbol)= $end); return $range; }
Very close to what I need to except I want a date range from $start to $end, spaced by the week or month. I think I could use createDateRangeArray and then use some kind of skip .
desired example.... createDateRangeArray(20070312,20070420,'Week') would result in (20070312,20070319,20070326,20070402,20070409,20070216) createDateRangeArray(20070312,20070420,'Month') would result in (20070312,20070412) anyone help with this ?
You could try John Geewax's version above and change the "+ 1 day" to "+ 1 month".
I like this
How would i go to group array by weeks ... each week would have correspoding day number. week2 | week3 | week4 | ... 1|2|3|... Also if no input given it should take current month and give a week number array, days array ... days would be grouped in each week array ... first is needed to find number of days for current month and later first and last date or is there any other better solution ? $week_nr['1']=array(1,2,3,4 ...); thanks
I don't quite understand what you're trying to do. Perhaps another reader of this blog will be able to help you.
Thanks a lot for this function! I had a really hideous nested loop parsing through years, months and days, and it had a bug I could not find, your function enabled me to delete most of my script, simplify it massivly and fix the bug!!
Cheers!
Thanks for this, just what I was looking for, but.....
Is there an easy way of limiting the returned dates to a single month. I.e. if the date range starts in one month and ends in another, only return the dates from the first. Thanks again
You could just let the function run, then loop through the results and stop when the previous value's first 7 characters don't match:
2008-12-30 2008-12-31 2009-01-01 (stop) Or you could modify the function to do this if you had to do it all the time.
Friends how i can validate a date range in a php form?
Example: actual date: 10/01/2009 the user only can input date from 05/01/2009 to 15/01/2009 (dd/mm/yyyy)
You'll want to use PHP form validation code and optionally some Javascript form validation. Basically you need to check the value of the submitted data in $_POST, and if it is not in your date range, return the form to the user with an error. You can use the Javascript to do the same validation in the user's browser so the data will be right before it is submitted. But it's best for the server to check anyway, because Javascript can be bypassed.
thaks for the date range function this what i am looking for my project
This place looks friendly & helpful, a php newb dare to ask if some can help use this function with a mysql_fetch_array to create an array eg:
Event date: 1st March every year. Start Date1, End Date1 -eg d1/m1/Yx d2/m2/Yy Star Date2, End Date2.. and so on Would it be possible to adapt this function to list number of events each date range contains.
This is a great handy little function. Although not a terribly difficult thing to do, it saved me time and you executed it as simply as possible - thanks!
I'm using it with jQuery and the Filament date range plugin: http://www.filamentgroup.com/lab/date_range_picker_using_jquery_ui_16_and_jquery_ui_css_framework/ Thanks
This broke at the start of the new year.
$Date1 = date('Y-n-j', strtotime('12/5/2009')); $Date2 = date('Y-n-j', strtotime('1/4/2010')); getDateRangeArray($Date1, $Date2); Dates will not go beyond 12/31/2009.
Oh man, thanks. That's pretty fundamental, but sometimes ya miss those
This is my version. It avoids any time zone issues, but no sanity checking. Be careful. Sorry but i don;t know how to do identation on this board.
function createMysqlDateRangeArray( $fromDate, $toDate) { $dateRange = (array) $fromDate; $splitDate = explode ( '-', $fromDate ); $count=0; while ( $dateRange[ sizeof($dateRange)-1 ] != $toDate ) { $splitDate[2]++; if ( ! checkdate( $splitDate[1], $splitDate[2], $splitDate[0] ) ) { $splitDate[2]=1; $splitDate[1]++; if ( ! checkdate( (int)$splitDate[1], (int)$splitDate[2], (int)$splitDate[0] ) ) { $splitDate[2]=1; $splitDate[1]=1; $splitDate[0]++; } } $dateRange[] = str_pad($splitDate[0], 4, "0", STR_PAD_LEFT) .'-'. str_pad($splitDate[1], 2, "0", STR_PAD_LEFT) .'-'. str_pad($splitDate[2], 2, "0", STR_PAD_LEFT); } return $dateRange; }
slightly better, again, time zone shifts aren't a problem
function createMysqlDateRangeArray( $fromDate, $toDate) { $dateRange = (array) $fromDate; $splitDate = explode ( '-', $fromDate ); while ( $dateRange[ sizeof($dateRange)-1 ] != $toDate ) { if ( ! checkdate( $splitDate[1], ++$splitDate[2], $splitDate[0] ) ) { if ( ! checkdate( (int)++$splitDate[1], (int)$splitDate[2]=1, (int)$splitDate[0] ) ) { $splitDate[2] = $splitDate[1] = 1; $splitDate[0]++; } } $dateRange[] = str_pad($splitDate[0], 4, "0", STR_PAD_LEFT) .'-'. str_pad($splitDate[1], 2, "0", STR_PAD_LEFT) .'-'. str_pad($splitDate[2], 2, "0", STR_PAD_LEFT); } return $dateRange; } |
CategoriesQuicksearchArchives |