Strange behavior of the date function! -- Please see.
Posted: Sun Nov 16, 2008 2:42 pm
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:
The function is:
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
Any idea as to why the dates are repeated? What can I do to avoid this bug/mistake?
Thanks
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
)
<
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);?>
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