Home  >  Article  >  Backend Development  >  How to convert time zone in PHP?

How to convert time zone in PHP?

Guanhui
GuanhuiOriginal
2020-06-15 17:15:523837browse

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 = &#39;Y-m-d H:i:s&#39;, $to = &#39;Europe/Rome&#39;, $from = &#39;Asia/Shanghai&#39;) {
    $datetime = new DateTime($date_time, new DateTimeZone($from));
    $datetime->setTimezone(new DateTimeZone($to));
    return $datetime->format($format);
}
 
$time = changeTimeZone(&#39;2018-12-19 00:00:00&#39;);
$t = changeTimeZone(&#39;2018-12-19 00:00:00&#39;, &#39;Y-m-d&#39;);
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn