简体   繁体   English

添加从GMT到Unix时间戳PHP的时间偏移量

[英]Add time offset from GMT to Unix timestamp PHP

I'd like to convert Unix timestamp from GMT to server time. 我想将Unix时间戳从GMT转换为服务器时间。 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 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! 这将消除你需要在函数中添加/减去GMT到/从时间戳等添加/减去GMT的需要!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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