UTC Offset in PHP
PhpTimezoneUtcPhp Problem Overview
What's the easiest way to get the UTC offset in PHP, relative to the current (system) timezone?
Php Solutions
Solution 1 - Php
date('Z');
returns the UTC offset in seconds.
Solution 2 - Php
// will output something like +02:00 or -04:00
echo date('P');
Solution 3 - Php
$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
Solution 4 - Php
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.
Solution 5 - Php
This is same JavaScript date.getTimezoneOffset()
function:
<?php
echo date('Z')/-60;
?>
Solution 6 - Php
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
Solution 7 - Php
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:
-
CST = Central Standard Time
-
CST = China Standard Time
-
CST = Cuba Standard Time
Solution 8 - Php
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.