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

How to convert time zone in PHP?

Guanhui
Release: 2023-03-01 15:28:01
Original
3856 people have browsed it

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;
Copy after login


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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template