Adam Ernst
Adam Ernst

Reputation: 54060

UTC Offset in PHP

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

Upvotes: 54

Views: 83203

Answers (8)

Jesse
Jesse

Reputation: 996

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:

Upvotes: 4

HMagdy
HMagdy

Reputation: 3295

Simply you can do this:

//Object oriented style
function getUTCOffset_OOP($timezone)
{
    $current   = timezone_open($timezone);
    $utcTime  = new \DateTime('now', new \DateTimeZone('UTC'));
    $offsetInSecs =  $current->getOffset($utcTime);
    $hoursAndSec = gmdate('H:i', abs($offsetInSecs));
    return stripos($offsetInSecs, '-') === false ? "+{$hoursAndSec}" : "-{$hoursAndSec}";
}

//Procedural style
function getUTCOffset($timezone)
{
    $current   = timezone_open($timezone);
    $utcTime  = new \DateTime('now', new \DateTimeZone('UTC'));
    $offsetInSecs =  timezone_offset_get( $current, $utcTime);
    $hoursAndSec = gmdate('H:i', abs($offsetInSecs));
    return stripos($offsetInSecs, '-') === false ? "+{$hoursAndSec}" : "-{$hoursAndSec}";
}


$timezone = 'America/Mexico_City';

echo "Procedural style<br>";
echo getUTCOffset($timezone); //-06:00
echo "<br>";
echo "(UTC " . getUTCOffset($timezone) . ") " . $timezone; // (UTC -06:00) America/Mexico_City
echo "<br>--------------<br>";
echo "Object oriented style<br>";
echo getUTCOffset_OOP($timezone); //-06:00
echo "<br>";
echo "(UTC " . getUTCOffset_OOP($timezone) . ") " . $timezone; // (UTC -06:00) America/Mexico_City

Upvotes: 12

Tuhin Bepari
Tuhin Bepari

Reputation: 745

// will output something like +02:00 or -04:00
echo date('P');

Upvotes: 53

Sos.
Sos.

Reputation: 967

This is same JavaScript date.getTimezoneOffset() function:

<?php
echo date('Z')/-60;
?>

Upvotes: 6

Kenny
Kenny

Reputation: 2176

I did a slightly modified version of what Oscar did.

date_default_timezone_set('America/New_York');
$utc_offset =  date('Z') / 3600;

This gave me the offset from my timezone, EST, to UTC, in hours.

The value of $utc_offset was -4.

Upvotes: 20

Amr
Amr

Reputation: 5159

date("Z") will return the UTC offset relative to the server timezone not the user's machine timezone. To get the user's machine timezone you could use the javascript getTimezoneOffset() function which returns the time difference between UTC time and local time, in minutes.

<script type="text/javascript">
    d = new Date();
    window.location.href = "page.php?offset=" + d.getTimezoneOffset();
</script>

And in page.php which holds your php code, you can do whatever you want with that offset value. Or instead of redirecting to another page, you can send the offset value to your php script through Ajax, according to your needs.

Upvotes: -2

Czimi
Czimi

Reputation: 2534

  date('Z');

returns the UTC offset in seconds.

Upvotes: 101

John Millikin
John Millikin

Reputation: 200806

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

Upvotes: 24

Related Questions