函数名:MongoDB\BSON\UTCDateTimeInterface::__toString()
适用版本:PHP 7.0.0 及以上版本
用法:该函数用于将UTCDateTimeInterface对象转换为字符串表示形式。
示例:
<?php
// 创建一个UTCDateTimeInterface对象
$utcDateTime = new MongoDB\BSON\UTCDateTime(1635345600000);
// 将UTCDateTimeInterface对象转换为字符串
$str = $utcDateTime->__toString();
echo $str; // 输出:2021-10-28T00:00:00.000000Z
?>
说明:
在示例中,我们首先创建了一个UTCDateTimeInterface对象,表示日期时间为2021年10月28日00:00:00 UTC。然后,通过调用__toString()
方法,将UTCDateTimeInterface对象转换为字符串表示形式。
注意事项:
__toString()
方法是在UTCDateTimeInterface接口中定义的,用于将UTCDateTimeInterface对象转换为字符串。因此,该方法只能在实现了UTCDateTimeInterface接口的类中使用。__toString()
方法返回的字符串格式为ISO 8601格式,包含日期和时间,以及毫秒和时区信息。