函数名:MongoDB\BSON\UTCDateTime::toDateTime()
函数功能:将UTCDateTime对象转换为PHP的DateTime对象。
适用版本:该函数在MongoDB扩展版本1.2.0及以上可用。
用法:
public MongoDB\BSON\UTCDateTime::toDateTime(): DateTime
示例:
// 创建一个UTCDateTime对象
$utcDateTime = new MongoDB\BSON\UTCDateTime(1618555200000); // 2021-04-16T00:00:00.000Z
// 将UTCDateTime对象转换为DateTime对象
$dateTime = $utcDateTime->toDateTime();
// 打印转换后的DateTime对象
echo $dateTime->format('Y-m-d H:i:s'); // 输出:2021-04-16 00:00:00
解释:
toDateTime()方法将UTCDateTime对象转换为PHP的DateTime对象。format()方法以指定的日期格式打印出转换后的日期时间。注意事项:
use语句引入MongoDB\BSON\UTCDateTime类。