How to convert between time zones in PHP using the DateTime class?

后端 未结 3 2006
北海茫月
北海茫月 2020-12-02 22:13

I am trying to convert time between current time to UTC and UTC to current time zone.

Here is what I have done:

$schedule_date = new DateTime($trigge         


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-02 22:42

    You are consuming the date/time and setting the time zone correctly, however before formatting the datetime, you are not setting the desired output timezone. Here is an example which accepts a UTC time zone, and converts the date/time to the America/Los_Angeles time zone:

    setTimeZone($target_timezone);
    
    // Outputs a date/time string based on the time zone you've set on the object.
    $triggerOn = $datetime->format('Y-m-d H:i:s');
    
    // Print the date/time string.
    print $triggerOn; // 2013-04-01 08:08:00
    

提交回复
热议问题