convert to UTC without changing php timezone settings

前端 未结 1 902
南旧
南旧 2021-01-06 10:21

How can I convert the time zone of a date string in php without changing the default time zone. I want to convert it locally to display only. The php time zone settting shou

相关标签:
1条回答
  • 2021-01-06 10:52
    $src_tz = new DateTimeZone('America/Chicago');
    $dest_tz = new DateTimeZone('America/New_York');
    
    $dt = new DateTime("2000-01-01 12:00:00", $src_tz);
    $dt->setTimeZone($dest_tz);
    
    echo $dt->format('Y-m-d H:i:s');
    

    Note that if the source time is UTC, you can change the one line to this:

    $dt = new DateTime("2000-01-01 12:00:00 UTC");
    

    Edit: Looks like you want to go to UTC. In that case, just use "UTC" as the parameter to the $dest_tz constructor, and use the original block of code. (And of course, you can omit the $src_tz parameter if it is the same as the default time zone.)

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