I am in Brazil and we changed to summer time this saturday.
I have a loop that genrates timestamps for midnight for every day in a selected timerange. So I get the timestam of the first day:
$start = 1413255600; // 2014-10-14 00:00:00
$end is some date in the future
And now I ahve a loop
for ($i = $start; $i <= $end; $i = strtotime('+ 1 day', $i)) {
echo date('Y-m-d H:i:s', $i);
echo "<br>";
}
This outputs:
2014-10-14 00:00:00
2014-10-15 00:00:00
2014-10-16 00:00:00
2014-10-17 00:00:00
2014-10-18 00:00:00
2014-10-19 01:00:00
2014-10-20 01:00:00
Shouldn't php/the server now that is there was the time change and add only 23 hours on the 19th? What am I doing wrong?
My timezone is correct and the server autimaticly changed to summertime on saturday.
I have a loop that genrates timestamps for midnight for every day in a selected timerange.
That's your first problem - assuming that midnight even exists. Whether or not that's true depends on your definition of midnight.
In the Brasilia time zone (and probably others in the region), 2014-10-19 00:00:00 didn't occur. The local times went:
2014-10-18 23:59:58
2014-10-18 23:59:59
2014-10-19 01:00:00
2014-10-19 01:00:01
So your script is doing the right thing - it's added one day to 2014-10-18 00:00:00
and come up with a reasonable answer: 2014-10-19 01:00:00
. You're then asking for one day after that value (1am) which is entirely reasonably 1am on the 20th.
If you want "the earliest time of day on each date" then you should make sure that $start
is a date which does have a midnight, then add 0 days, 1 day, 2 days, 3 days etc to $start
- rather than repeatedly adding one day. I'm not a PHP developer, but it seems to me that using DateTime::Add
would be a better approach to this than using strtotime
.
See more on this question at Stackoverflow