Pablo Assis
Pablo Assis

Reputation: 47

Workaround to find exact midnight within summertime in Php7

Ok, this is definitely a PHP 7 bug. My question is: do you know any reliable workaround?

$date1 = new DateTime('2017-10-14', new DateTimeZone('America/Sao_Paulo'));
print_r($date1);
print_r('<br/>');
print_r('<br/>');
$date2 = new DateTime('2017-10-15', new DateTimeZone('America/Sao_Paulo'));
print_r($date2);
print_r('<br/>');
print_r('<br/>');
$date3 = new DateTime('2017-10-16', new DateTimeZone('America/Sao_Paulo'));
print_r($date3);

This would give me midnight for each day, right? Wrong.

DateTime Object ( [date] => 2017-10-14 00:00:00.000000 [timezone_type] => 3 [timezone] => America/Sao_Paulo ) 

DateTime Object ( [date] => 2017-10-15 01:00:00.000000 [timezone_type] => 3 [timezone] => America/Sao_Paulo ) 

DateTime Object ( [date] => 2017-10-16 00:00:00.000000 [timezone_type] => 3 [timezone] => America/Sao_Paulo )

It is one in the morning on the 15th. Is putting the offset to -2 and adding one hour. It happens always in the 15th. Phpversion is 7.0.22

I really need to be able to reliably find midnight! :)

Upvotes: 0

Views: 55

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241563

It is one in the morning on the 15th... I really need to be able to reliably find midnight!

You can't. There is no local midnight in São Paulo on October 15th, 2017. It doesn't exist. As the local time approached midnight, it moved forward to 1:00 am for the start of daylight saving time. Counting in microseconds, it went as follows:

..
2017-10-14 23:59:59.999998 UTC-03:00 (BRT)
2017-10-14 23:59:59.999999 UTC-03:00 (BRT)
2017-10-15 01:00:00.000000 UTC-02:00 (BRST)
2017-10-15 01:00:00.000001 UTC-02:00 (BRST)
...

Refer to this site for a visualization.

Upvotes: 2

Related Questions