Question

Pretty straightforward:

PHP returns timezones in the following format:

"America/Los_Angeles"

I want to convert it to:

-7

I found many codes to convert it, calculating differences with custom functions.. but as a minimalist I can't believe there isn't a simpler way (perhaps a datetime function) to get the value right away.. anyone know about this?

Was it helpful?

Solution

$dateTimeZone = new DateTimeZone("America/Los_Angeles");
$date = new DateTime(null, $dateTimeZone);
echo $dateTimeZone->getOffset($date)/60/60;

Demo

OTHER TIPS

PHP uses the standard time zones from the IANA time zone database.

"America/Los_Angeles" is not the same as -7. -7 is a "time zone offset", not a "time zone". Read "Time Zone != Offset" in the timezone tag wiki.

Consider that "America/Los_Angeles" represents US Pacific Time, which is only -7 during Pacific Daylight Time (PDT). The rest of the year, it is in Pacific Standard Time (PST), which is -8.

The code provided in John's answer will correctly give you the offset that a time zone uses for the current date and time. Recognize that the value will change depending on when you run the code.

Also, you should realize that while you can determine the offset for a particular time zone at any particular moment, the reverse is not true. You cannot treat -7 as a time zone and expect someone to be able to tell that it is Pacific Daylight Time. Consider that -7 also applies to Mountain Standard Time, which Arizona uses year-round.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top