PHP如何转换时区?-PHP问题

资源魔 32 0

PHP若何转换时区?

起首实例化“DateTimeZone”类,实例化参数为转化的时区;而后实例化“DateTime”类,其参数1为要转换的工夫,参数2为“DateTimeZone”工具;最初挪用“DateTime”工具的format便可。

代码示例

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


保举教程:《PHP》

以上就是PHP若何转换时区?的具体内容,更多请存眷资源魔其它相干文章!

标签: php php教程 php故障解决 php使用问题

抱歉,评论功能暂时关闭!