简体   繁体   English

在PHP中将时区从UTC转换为ET

[英]Converting timezone from UTC to ET in PHP

I tried to convert timezone from UTC to ET .It is working perfectly fine in my local host(windows) but in Production(Linux server) when I convert UTC(1:20 pm ) to ET.File_date is showing as 9.20 AM. 我试图将时区从UTC转换为ET。当我将UTC(1:20 pm)转换为ET时,它在我的本地主机(windows)上工作正常,但在Production(Linux服务器)中工作正常.File_date显示为9.20 AM。 Any idea as why it is showing wrong time? 知道为什么它显示错误的时间吗?

            $original_datetime = date('Y-m-d H:i:s');

            $original_timezone = new DateTimeZone('UTC');

            $datetime = new DateTime($original_datetime, $original_timezone);

            $target_timezone = new DateTimeZone('America/New_York');

            $datetime->setTimeZone($target_timezone);

            $file_date = $datetime->format("m-d-Y g:i A");

That is not the wrong time. 那不是错误的时间。

Eastern Time and UTC are four hours apart at the moment. 目前,东部时间和世界标准时间相隔四个小时。 1:20 PM in UTC is 9:20 AM in Eastern US Time (ET). UTC的1:20 PM是美国东部时间(ET)的9:20 AM。 So the results you're getting in production are correct. 因此,您在生产中获得的结果是正确的。

Step through this code on your local host, and check each of the variables you use. 在本地主机上单步执行此代码,并检查您使用的每个变量。 See which one is being set incorrectly. 查看哪个设置不正确。 Then you'll know where the problem lies. 然后,您将知道问题出在哪里。

I suspect the problem is that the first line should be a call to gmdate() instead of date() . 我怀疑问题是第一行应该是对gmdate()的调用,而不是date()的调用。 gmdate() gives you the current time in GMT, but date() gives it to your in your local time. gmdate()会为您提供格林尼治标准时间的当前时间,而date()会为您提供本地时间的当前时间。 This could be the difference if your production server is set to GMT/UTC by default. 如果您的生产服务器默认设置为GMT / UTC,则可能会有所不同。

I would like to reiterate, however, that it is your local result that is incorrect - not your production result. 但是,我想重申的是,您当地的结果不正确-而不是您的生产结果。 9:20 AM is the proper conversion of 1:20 PM from UTC to America/New_York. 9:20 AM是将UTC的1:20 PM正确转换为America / New_York。

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

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