您的位置 首页 技术

PHP如何转换时区?

PHP如何转换时区? 首先实例化“DateTimeZone”类,实例化参数为转化的时区;然后实例化“DateTime”类,其参数1为要转换的时间,参数2为“DateTimeZone…

PHP如何转换时区?

首先实例化“DateTimeZone”类,实例化参数为转化的时区;然后实例化“DateTime”类,其参数1为要转换的时间,参数2为“DateTimeZone”对象;最后调用“DateTime”对象的format即可。

代码示例

<?phpfunction 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;

推荐教程:《PHP》

以上就是PHP如何转换时区?的详细内容,更多请关注24课堂在线网其它相关文章!

本文来自网络,不代表24小时课堂在线立场,转载请注明出处:https://www.24ketang.cn/78195.html

为您推荐

返回顶部