Home > Article > Backend Development > How to convert time zone in PHP?
How to convert time zone in PHP?
First instantiate the "DateTimeZone" class, and the instantiation parameter is the time zone to be converted; then instantiate the "DateTime" class, whose parameter 1 is the time to be converted, and parameter 2 is the "DateTimeZone" object; Finally, call the format of the "DateTime" object.
Code sample
<?php function changeTimeZone($date_time, $format = 'Y-m-d H:i:s', $to = 'Europe/Rome', $from = 'Asia/Shanghai') { $datetime = new DateTime($date_time, new DateTimeZone($from)); $datetime->setTimezone(new DateTimeZone($to)); return $datetime->format($format); } $time = changeTimeZone('2018-12-19 00:00:00'); $t = changeTimeZone('2018-12-19 00:00:00', 'Y-m-d'); echo $time; echo $t;
Recommended tutorial: "PHP"
The above is the detailed content of How to convert time zone in PHP?. For more information, please follow other related articles on the PHP Chinese website!