Jump to content

How many seconds in a week?!?!


mapleleaf

Recommended Posts

<?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??

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.