php 获取美国/东部当前时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4456395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:05:29  来源:igfitidea点击:

php get US/Eastern current time

php

提问by booota

I want to get current time of US/Eastern timezone. How would I achieve that.

我想获取美国/东部时区的当前时间。我将如何实现这一目标。

I have tried following code but it is displaying my system's time.

我试过以下代码,但它显示了我的系统时间。

<?php
   date_default_timezone_set('US/Eastern');
   $currenttime = date('h:i:s:u');
   list($hrs,$mins,$secs,$msecs) = split(':',$currenttime);
   //print "&time2=".$secs."&time1=".$mins."&time0=".$hrs;
?>

I am using this script with flash so commented out 'print' line.

我在 flash 中使用此脚本,因此注释掉了“打印”行。

回答by miku

<?php
   echo date_default_timezone_get();
   $currenttime = date('h:i:s:u');
   list($hrs,$mins,$secs,$msecs) = split(':',$currenttime);
   echo " => $hrs:$mins:$secs\n";

   date_default_timezone_set('US/Eastern');
   echo date_default_timezone_get();
   $currenttime = date('h:i:s:u');
   list($hrs,$mins,$secs,$msecs) = split(':',$currenttime);
   echo " => $hrs:$mins:$secs\n";

   date_default_timezone_set('America/New_York');
   echo date_default_timezone_get();
   $currenttime = date('h:i:s:u');
   list($hrs,$mins,$secs,$msecs) = split(':',$currenttime);
   echo " => $hrs:$mins:$secs\n";

?>

Seems to work here (in Berlin):

似乎在这里工作(在柏林):

Europe/Berlin => 01:42:42
US/Eastern => 07:45:18
America/New_York => 07:45:18

回答by Sajeev Nair

$amNY = new DateTime('America/New_York');
$estTime = $amNY->format('h:i:s:u');

or if you are using php 5.4 and above

或者如果您使用的是 php 5.4 及更高版本

estTime = (new DateTime('America/New_York'))->format('h:i:s:u');

date_default_timezone_set()will affect the whole scriptand should be used carefully

date_default_timezone_set()会影响整个脚本,应谨慎使用

回答by Robert

http://www.php.net/manual/en/timezones.php

http://www.php.net/manual/en/timezones.php

Looks like the US/Easternis deprecated. Try America/New_York

看起来US/Eastern已弃用。尝试America/New_York

EDITthis probably won't fix your problem, but you should do it anyway. Being deprecated means that they could remove it in the future.

编辑这可能不会解决您的问题,但无论如何您都应该这样做。被弃用意味着他们可以在未来将其删除。

回答by themicahmachine

The easiest way is probably to use gmmktime()to get the Unix timestampfor the current GMT, then subtract 5 hoursfrom it. That way you get Eastern Time no matter where the server happens to be.

最简单的方法可能是使用gmmktime()获取Unix timestamp当前 GMT 的 ,然后subtract 5 hours从中获取。这样,无论服务器在哪里,您都可以获得东部时间。

回答by Kingdru

I just use

我只是用

<?php
Date_default_timezone('est');
$lv=date('l, F jS, Y, g:I:s A T,$ir['laston']);
?>
<b>last visit</b> <em>$lv.</em>

Seems to work perfectly for me on my game that I'm working on. Mind you if you want to use it just remove the $ir['laston'] and it should work no problem

似乎在我正在开发的游戏中非常适合我。请注意,如果您想使用它,只需删除 $ir['laston'] 并且它应该没问题