Page 1 of 1

Strange behavior of the date function! -- Please see.

Posted: Sun Nov 16, 2008 2:42 pm
by cristiano
Hey all,

Just found out a strange behavior of the date function.

I used a function to print dates of a week -- it takes a date as input and returns an array containing that particular week's date. I took this function somewhere in slashdot i guess. I don't have the link handy. I assumed Sunday to be start of the week.

The thing is, if I pass the input as 2008-11-02 ( November, 2nd 2008, Sunday!) the returned array is:

Code: Select all

 
Array
(
    [0] => 2008-11-02
    [1] => 2008-11-02
    [2] => 2008-11-03
    [3] => 2008-11-04
    [4] => 2008-11-05
    [5] => 2008-11-06
    [6] => 2008-11-07
)
<
 
The function is:

Code: Select all

 
function week_from_sunday($startdate) {
    // Assuming $startdate is in format YYYY-MM-DD
    list($year, $month, $day) = explode("-", $startdate);
 
    // Get the weekday of the given date
    $wkday = date('l',mktime('0','0','0', $month, $day, $year));
 
    switch($wkday) {
        case 'Sunday': $numDaysToSun = 0; break;
        case 'Monday': $numDaysToSun = 1; break;
        case 'Tuesday': $numDaysToSun = 2; break;
        case 'Wednesday': $numDaysToSun = 3; break;
        case 'Thursday': $numDaysToSun = 4; break;
        case 'Friday': $numDaysToSun = 5; break;
        case 'Saturday': $numDaysToSun = 6; break;   
    }
 
    // Timestamp of the sunday for that week
    $sunday = mktime('0','0','0', $month, $day-$numDaysToSun, $year);
    
    $seconds_in_a_day = 86400;
    // Get date for 7 days from Sunday (inclusive)
    echo "<table><tr><td>Time-to-be-added</td> <td>Time-of-the-date-in-seconds</td> <td>Date</td><br>";
    for($i=0; $i<7; $i++)
    {   
        $tobeadded=$seconds_in_a_day*$i;
        $time=$sunday+$tobeadded;
        $dates[$i] = date('Y-m-d',$time);
        echo "<tr><td>".$tobeadded."</td><td> ".$time."</td><td>".$dates[$i]."</td></tr>";
    }
    echo "</table>";
    return $dates;
}
 
$date=week_from_sunday("2008-11-02");
print_r($date);?>
 
Now why on earth would that happen? I tried lot of other start dates -- i.e. other weeks. Its working fine for other weeks. But for this week alone, the second date is the same as the first one.

I would assume this is prolly due to the input seconds from the Unix Epoch.

I also printed the values of the seconds that are used to create the dates. Those came out as below

Code: Select all

 
Time-to-be-added    Time-of-the-date-in-seconds     Date
0                           1225609200                          2008-11-02
86400                    1225695600                          2008-11-02
172800                   1225782000                          2008-11-03
259200                   1225868400                          2008-11-04
345600                   1225954800                          2008-11-05
432000                   1226041200                          2008-11-06
518400                   1226127600                          2008-11-07
 

Any idea as to why the dates are repeated? What can I do to avoid this bug/mistake?

Thanks

Re: Strange behavior of the date function! -- Please see.

Posted: Sun Nov 16, 2008 3:25 pm
by Mark Baker
Out of interest (not being an American, I don't know the actual date), what date did DST end this year?

I think you'll find it works if you change the hour parameter used for the two mktime() calls to 1 or above (to allow for the hour change)

Re: Strange behavior of the date function! -- Please see.

Posted: Sun Nov 16, 2008 3:47 pm
by cristiano
Whoa!

That was great!

It worked.

Care to explain why?


Also, I'm not American. I'm from India

Re: Strange behavior of the date function! -- Please see.

Posted: Sun Nov 16, 2008 5:16 pm
by requinix
Mark Baker got it right: the problem was daylight savings, which (at least in America) ended two weeks ago. That'd be November 2nd.

In the fall, daylight savings goes back an hour at 2am. So if you add 24 hours starting at midnight Nov. 2nd you get 11pm Nov. 2nd: it would normally be 12am Nov. 3rd but an hour was taken off.
So the second day would be Nov. 2nd as well.

Like he said, if you use a time like 1am instead of 12am, whenever daylight savings hits, whether it starts or stops, the date will stay the same.