English | 简体中文 | 繁體中文
查询

MongoDB\BSON\UTCDateTimeInterface::toDateTime()函数—用法及示例

「 将MongoDB\BSON\UTCDateTime对象转换为PHP的DateTime对象 」


函数名:MongoDB\BSON\UTCDateTimeInterface::toDateTime()

适用版本:PHP 7.0.0 及以上版本

用法:该方法用于将MongoDB\BSON\UTCDateTime对象转换为PHP的DateTime对象。

示例:

<?php
// 创建一个UTCDateTime对象
$utcDateTime = new MongoDB\BSON\UTCDateTime(1609459200000);

// 转换为DateTime对象
$dateTime = $utcDateTime->toDateTime();

// 打印转换后的DateTime对象
echo $dateTime->format('Y-m-d H:i:s');
?>

输出:

2021-01-01 00:00:00

解释: 在示例中,我们首先创建了一个UTCDateTime对象,其表示的时间戳为1609459200000,即1970年1月1日以来的毫秒数。然后,我们使用toDateTime()方法将UTCDateTime对象转换为PHP的DateTime对象。最后,我们使用DateTime对象的format()方法将其格式化为指定的日期和时间格式,并打印输出结果。在本例中,输出结果为"2021-01-01 00:00:00",表示转换后的DateTime对象表示的日期和时间为2021年1月1日 00:00:00。

补充纠错
热门PHP函数
分享链接