编程语言
首页 > 编程语言> > php中不同时区的时间戳

php中不同时区的时间戳

作者:互联网

在下面的代码中,我需要获得2个国家/地区时区的unixtimestamp.代码的输出将给我一个差异的日期,但时间戳彼此不同.它保持不变.任何人都可以提供解决方案来获得不同时区的不同时间戳吗?提前致谢.

date_default_timezone_set('Asia/Calcutta');
echo date("Y-m-d H:i:s")."<br/>"; //2012-12-18 12:12:12
echo strtotime(date("Y-m-d H:i:s",time()))."<br/>"; //1355812934

date_default_timezone_set('Europe/London');
echo date("Y-m-d H:i:s")."<br/>"; //2012-12-18 06:12:12
echo strtotime(date("Y-m-d H:i:s",time()))."<br/>"; //1355812934

解决方法:

您可以使用日期(“Z”)以秒为单位获取时区偏移.然后根据需要进行计算.

date_default_timezone_set('Asia/Calcutta');
echo 'Local time : '.date("r").'<br>'; // local time
echo 'Offset : '.date("Z").'<br>'; // time zone offset from UTC in seconds 
echo 'UTC Time : '.date('r', strtotime(date("r")) + (date("Z")*-1)); echo '<br><br>'; // this is UTC time converted from Local time

date_default_timezone_set('Europe/London');
echo 'Local time : '.date("r").'<br>'; // local time
echo 'Offset : '.date("Z").'<br>'; // time zone offset from UTC in seconds 
echo 'UTC time : '.date('r', strtotime(date("r")) + (date("Z")*-1)); echo '<br><br>'; // this is utc time converted from Local time

输出:

Local time : Tue, 18 Dec 2012 10:53:07 +0530
Offset : 19800
UTC Time : Tue, 18 Dec 2012 05:23:07 +0530

Local time : Tue, 18 Dec 2012 05:23:07 +0000
Offset : 0
UTC time : Tue, 18 Dec 2012 05:23:07 +0000  

标签:php,timezone,datetime,time,unix-timestamp
来源: https://codeday.me/bug/20190901/1783294.html