James
James

Reputation: 478

Getting correct time based on timezoneid using PHP

I'm trying to use the accurate current local time for any location in the world based on their "TimezoneId". Somestimes I'm getting the correct GMT offset. For example:

$localTimezone = new DateTimeZone('America/Los_Angeles');
$gmtTimezone = new DateTimeZone('GMT');
$currentDateTime = new DateTime('2020-01-21 13:14', $gmtTimezone);
$offset = $localTimezone ->getOffset($currentDateTime );
echo $offset/3600;

This returns -8 which is correct for today's date in Los Angeles.

However, when I try the same using America/Sao_Paulo as the "TimezoneId" I get -2 which is currently not correct for this location. It should be -3 according to Google and other time apps I have tried.

I've searched for a solution and seen different attemps which don't seem to be working me.

Thank you for your input!

Upvotes: 0

Views: 165

Answers (1)

Álvaro González
Álvaro González

Reputation: 146390

According to timeanddate.com Brazil stopped observing DST on 17 February 2019. To get that reflected in your application you need a PHP release that includes a version of the IANA Time Zone Database updated after whenever the announcement was made. I've run your code in 3v4l.org and got 7.3.9 - 7.4.1.

Upvotes: 3

Related Questions