Hướng dẫn dùng timezone offsets trong PHP

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

Nội dung chính

  • Not the answer you're looking for? Browse other questions tagged php timezone utc or ask your own question.
  • How do I get timezone offset?
  • How do I get user time zones?
  • How do I get client browser timezone at end of PHP?
  • What is offset timezone?

Nội dung chính

  • Not the answer you're looking for? Browse other questions tagged php timezone utc or ask your own question.
  • How do I get timezone offset?
  • How do I get user time zones?
  • How do I get client browser timezone at end of PHP?
  • What is offset timezone?

asked Oct 11, 2008 at 0:58

Adam ErnstAdam Ernst

50.3k18 gold badges58 silver badges71 bronze badges

  date('Z');

returns the UTC offset in seconds.

answered Oct 11, 2008 at 1:06

4

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

Ole V.V.

77.5k15 gold badges122 silver badges146 bronze badges

answered Mar 15, 2016 at 20:17

2

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

answered Oct 11, 2008 at 1:06

John MillikinJohn Millikin

193k39 gold badges211 silver badges222 bronze badges

1

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.

answered Jun 9, 2015 at 15:08

KennyKenny

2,1202 gold badges21 silver badges30 bronze badges

This is same JavaScript date.getTimezoneOffset() function:


answered May 29, 2017 at 21:33

زيادزياد

89210 silver badges14 bronze badges

19

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
"; echo getUTCOffset($timezone); //-06:00 echo "
"; echo "(UTC " . getUTCOffset($timezone) . ") " . $timezone; // (UTC -06:00) America/Mexico_City echo "
--------------
"; echo "Object oriented style
"; echo getUTCOffset_OOP($timezone); //-06:00 echo "
"; echo "(UTC " . getUTCOffset_OOP($timezone) . ") " . $timezone; // (UTC -06:00) America/Mexico_City

answered Mar 23, 2018 at 17:31

HMagdyHMagdy

2,87133 silver badges52 bronze badges

2

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

echo date('O');

This may be useful for a proper RSS RFC822 format

Sat, 07 Sep 2002 00:00:01 -0500

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

answered Apr 7 at 5:23

Jesse יִשַׁיJesse יִשַׁי

6051 gold badge7 silver badges22 bronze badges

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.


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.

answered Jan 5, 2013 at 15:55

AmrAmr

4,3536 gold badges44 silver badges58 bronze badges

3

Not the answer you're looking for? Browse other questions tagged php timezone utc or ask your own question.

How do I get timezone offset?

Definition and Usage getTimezoneOffset() returns the difference between UTC time and local time. getTimezoneOffset() returns the difference in minutes. For example, if your time zone is GMT+2, -120 will be returned.

How do I get user time zones?

You can set any specific time zone using the PHP function date_default_timezone_set . This sets the specified time zone for users. Basically the users' time zone is goes to the client side, so we must use JavaScript for this. Below is the script to get users' time zone using PHP and JavaScript.

How do I get client browser timezone at end of PHP?

Detecting the User Timezone Name Detecting the user timezoone is a two-step process: Get the timezone offset of the browser with Javascript, and sent it to PHP (via AJAX or something). Convert the timezone offset to a valid TZ Database timezone name such as America/New_York, Asia/Kolkata.

What is offset timezone?

What is a "zone offset"? A zone offset is the difference in hours and minutes between a particular time zone and UTC. In ISO 8601, the particular zone offset can be indicated in a date or time value. The zone offset can be Z for UTC or it can be a value "+" or "-" from UTC.