A simple code to print a date
$first_date = 1319493600;
$last_date = 1320184800;
$testDate = $first_date;
while ($testDate<=$last_date)
{
echo strftime("%a", $testDate).' '. strftime("%d", $testDate).' '.strftime("%b", $testDate).'<br>';
$testDate += 86400;
}
Yes this simple script fails me as you see below
Tue 25 Oct
Wed 26 Oct
Thu 27 Oct
Fri 28 Oct
Sat 29 Oct
Sun 30 Oct <----Error
Sun 30 Oct <----Error
Mon 31 Oct
Tue 01 Nov
Anyone has any idea why?
This is, in fact, the reason why you should never increment the day by adding 24 hours to the timestamp. Chances are that you are in a timezone that observes daylight saving time and the last Sunday of October is not 24 hours long.
You can confirm this by adding
echo strftime("%r %x", $testDate);inside the loop. (Also note that you can use multiple modifiers instrftime()so you don’t have to concatenate them:strftime("%a %d %b", $testDate);.)Always use date functions like
strtotime()ormktime()to increment the date.