How to get local time in php?

前端 未结 7 1271
野性不改
野性不改 2021-02-02 03:12

I am trying to get the local time using php. I wrote two different versions, but they both give the wrong time

date_default_timezone_set(\'UTC\');
$now = new Dat         


        
7条回答
  •  梦如初夏
    2021-02-02 03:55

    DateTime::getTimestamp() returns unix timestamp. That number is always UTC. What you want is format the date according to your time zone.

    $dt = new DateTime("now", new DateTimeZone('America/New York'));
    
    echo $dt->format('m/d/Y, H:i:s');
    

    Or use a different date format, according to what you need.

    Also, you can use DateTime library for all your date needs. No need to change server's default timezone every time you want to fetch the date.

提交回复
热议问题