The MongoDB\BSON\UTCDateTime class

(mongodb >=1.0.0)

简介

Represents a » BSON date. The value is a 64-bit integer that represents the number of milliseconds since the Unix epoch (Jan 1, 1970). Negative values represent dates before 1970.

类摘要

MongoDB\BSON\UTCDateTime implements MongoDB\BSON\UTCDateTimeInterface , MongoDB\BSON\Type , Serializable , JsonSerializable {
/* 方法 */
final public __construct ([ integer|float|string|DateTimeInterface $milliseconds = NULL ] )
final public mixed jsonSerialize ( void )
final public string serialize ( void )
final public DateTime toDateTime ( void )
final public string __toString ( void )
final public void unserialize ( string $serialized )
}

更新日志

版本 说明
1.2.0 Implements Serializable and JsonSerializable.

Table of Contents

User Contributed Notes

mark at DONTSPAM dot moderndeveloperllc dot com 27-Oct-2016 11:09
Be careful using UTCDateTime if you need microsecond precision as this class only stores time to the _millisecond_. You need to store the date as a string in MongoDB if you want the full precision. Casting DateTime to a float can also remove precision.

An easy way to see the differences is with:

Code:
var_dump(
  (new DateTime)->format('U.u'),
  (float) (new DateTime)->format('U.u'),
  (new MongoDB\BSON\UTCDateTime())->toDateTime()->format('U.u')
);

Output:
string(17) "1477534060.918415"
double(1477534060.9185)
string(17) "1477534060.918000"

NOTE: Lots of bugs concerning microsecond formatting for DateTimeInterface classes were fixed as of PHP 7.1.0 RC4. The above code is from PHP 7.1.0 RC5 and mongodb 1.2.0alpha3. The 1.2.0 version of the extension added the ability to instantiate this class without passing integer milliseconds.