UTC Offset in PHP

Viewed 72920

What's the easiest way to get the UTC offset in PHP, relative to the current (system) timezone?

8 Answers
  date('Z');

returns the UTC offset in seconds.

timezone_offset_get()

$this_tz_str = date_default_timezone_get();
$this_tz = new DateTimeZone($this_tz_str);
$now = new DateTime("now", $this_tz);
$offset = $this_tz->getOffset($now);

Untested, but should work

This will output something formatted as: +0200 or -0400:

echo date('O');

This may be useful for a proper RSS RFC822 format

<pubDate>Sat, 07 Sep 2002 00:00:01 -0500</pubDate>

GMT offsets (like this) shouldn't use a colon (+02:00 from date('P');).

And, although it is acceptable for RSS RFC833, we don't want output like PDT and CST because these are arbitraty and "CST" can mean many things:

Related