Convert UTC dates to local time in PHP

前端 未结 10 1598
青春惊慌失措
青春惊慌失措 2020-11-28 07:16

I\'m storing the UTC dates into the DB using:

$utc = gmdate(\"M d Y h:i:s A\");

and then I want to convert the saved UTC date to the client

10条回答
  •  隐瞒了意图╮
    2020-11-28 07:30

    PHP's strtotime function will interpret timezone codes, like UTC. If you get the date from the database/client without the timezone code, but know it's UTC, then you can append it.

    Assuming you get the date with timestamp code (like "Fri Mar 23 2012 22:23:03 GMT-0700 (PDT)", which is what Javascript code ""+(new Date()) gives):

    $time = strtotime($dateWithTimeZone);
    $dateInLocal = date("Y-m-d H:i:s", $time);
    

    Or if you don't, which is likely from MySQL, then:

    $time = strtotime($dateInUTC.' UTC');
    $dateInLocal = date("Y-m-d H:i:s", $time);
    

提交回复
热议问题