D
D
Dealaxer2016-08-22 19:01:34
PHP
Dealaxer, 2016-08-22 19:01:34

PHP How to output time zone GMT - or +?

Welcome all.
Knowing the time zone, well, for example, in this format: "America/Los_Angeles", how to display data like "GMT-10" in a variable, and you need to know exactly the zone - or +.
I fight for a long time, I do not know what to apply.
Is there a method? Or does it require complex manipulations?
Thank you in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
toxa82, 2016-08-22
@Dealaxer

echo date('O'); // +0300
echo date('P'); // +03:00

there is also an offset in seconds date('Z'), but it is displayed without the + sign. You can use sprintf for formatting, for example:
PS The last option is not entirely correct, because it shows whole numbers, although there are countries whose GMT differs by half an hour/quarter. For example, Australia (UTC + 10:30 - Lord Howe Island) and New Zealand (UTC + 12:45 - Chatham Archipelago) and many others . So it is more preferable and correct to use date('O') or date('P').

D
Dealaxer, 2016-08-22
@Dealaxer

Solution:
1)

$dtz = new DateTimeZone('America/Los_Angeles');
$zona = new DateTime('now', $dtz);
$offset = $dtz->getOffset($zona) / 3600;
echo "GMT" . ($offset < 0 ? $offset : "+".$offset);

2)
$target_time_zone = new DateTimeZone('America/Los_Angeles');
$date_time = new DateTime('now', $target_time_zone);
echo 'GMT '.$date_time->format('P');

Only, they are too bulky, is there something one-liner?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question