php get US/Eastern current time

前端 未结 5 1503
半阙折子戏
半阙折子戏 2020-12-13 15:55

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.

<         


        
相关标签:
5条回答
  • 2020-12-13 16:02
    $amNY = new DateTime('America/New_York');
    $estTime = $amNY->format('h:i:s:u');
    

    or if you are using php 5.4 and above

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

    date_default_timezone_set() will affect the whole script and should be used carefully

    0 讨论(0)
  • 2020-12-13 16:09

    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

    0 讨论(0)
  • 2020-12-13 16:16

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

    0 讨论(0)
  • 2020-12-13 16:22
    <?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
    
    0 讨论(0)
  • 2020-12-13 16:22

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

    Looks like the US/Eastern is deprecated. Try America/New_York

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

    0 讨论(0)
提交回复
热议问题