简体   繁体   中英

Add time offset from GMT to Unix timestamp PHP

I'd like to convert Unix timestamp from GMT to server time. How to convert the time offset to seconds in order to add it to the timestamp? Or is there any better way?

$t = 1470565421;
$off = (new DateTime('now', new DateTimeZone(date_default_timezone_get())))->format('P'); // returns +02:00
$t2 = $t + convertToSec($off);

Depending on the user, set the default timezone using

http://php.net/manual/en/function.date-default-timezone-set.php

date_default_timezone_set('America/Los_Angeles');

That will take away your need to add/substract GMT to/from timestamps etc whithin functions!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM