mapleleaf Posted July 9, 2012 Share Posted July 9, 2012 <?php function get_weekly($start, $end) { $dates = array(); while($start < $end) { array_push($dates,date("D m y",$start)); $start = $start + (86400 * 7); // perhaps erroneous concept of the number of seconds in a week } return $dates; } $start = strtotime("Tue 10th Jul 2012"); $end = strtotime("Tue 10th Jul 2013"); echo "<pre>"; print_r(get_weekly($start, $end)); echo "</pre"; ?> And the results which I think amount to a bug in PHP: Array ( [0] => Tue 07 12 [1] => Tue 07 12 [2] => Tue 07 12 [3] => Tue 07 12 [4] => Tue 08 12 [5] => Tue 08 12 [6] => Tue 08 12 [7] => Tue 08 12 [8] => Tue 09 12 [9] => Tue 09 12 [10] => Tue 09 12 [11] => Tue 09 12 [12] => Tue 10 12 [13] => Tue 10 12 [14] => Tue 10 12 [15] => Tue 10 12 [16] => Tue 10 12 [17] => Mon 11 12 [18] => Mon 11 12 [19] => Mon 11 12 [20] => Mon 11 12 [21] => Mon 12 12 [22] => Mon 12 12 [23] => Mon 12 12 [24] => Mon 12 12 [25] => Mon 12 12 [26] => Mon 01 13 [27] => Mon 01 13 [28] => Mon 01 13 [29] => Mon 01 13 [30] => Mon 02 13 [31] => Mon 02 13 [32] => Mon 02 13 [33] => Mon 02 13 [34] => Mon 03 13 [35] => Tue 03 13 [36] => Tue 03 13 [37] => Tue 03 13 [38] => Tue 04 13 [39] => Tue 04 13 [40] => Tue 04 13 [41] => Tue 04 13 [42] => Tue 04 13 [43] => Tue 05 13 [44] => Tue 05 13 [45] => Tue 05 13 [46] => Tue 05 13 [47] => Tue 06 13 [48] => Tue 06 13 [49] => Tue 06 13 [50] => Tue 06 13 [51] => Tue 07 13 [52] => Tue 07 13 ) How did Monday get in there?? Quote Link to comment https://forums.phpfreaks.com/topic/265405-how-many-seconds-in-a-week/ Share on other sites More sharing options...
DavidAM Posted July 9, 2012 Share Posted July 9, 2012 How many seconds in a week depends entirely on what week you are talking about. There are two weeks in each year that will have 1 hour more or less than the usual -- the weeks when Daylight Saving Time changes. The best approach for your situation is to use strtotime : $start = strtotime("+1 week", $start); instead of adding some number of seconds. Quote Link to comment https://forums.phpfreaks.com/topic/265405-how-many-seconds-in-a-week/#findComment-1360212 Share on other sites More sharing options...
Psycho Posted July 9, 2012 Share Posted July 9, 2012 The best approach for your situation is to use strtotime : $start = strtotime("+1 week", $start); instead of adding some number of seconds. ^^ - I'd agree with that. But, I've seen some quirky things with strtotime() when adding/subtracting as well. When trying to move forward/back a day at a time it is always best, IMO, to always normalize the time to be noon. That way the results will not be affected by Daylight Savings time. Quote Link to comment https://forums.phpfreaks.com/topic/265405-how-many-seconds-in-a-week/#findComment-1360213 Share on other sites More sharing options...
mapleleaf Posted July 9, 2012 Author Share Posted July 9, 2012 Thanks both. I have also seen some strange things with strtotime but it is still the best option here I suppose. Quote Link to comment https://forums.phpfreaks.com/topic/265405-how-many-seconds-in-a-week/#findComment-1360216 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.