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.
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! |
CategoriesQuicksearchArchives |