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.

类摘要

final class MongoDB\BSON\UTCDateTime implements MongoDB\BSON\UTCDateTimeInterface, MongoDB\BSON\Type, Serializable, JsonSerializable, Stringable {
/* 方法 */
final public __construct(int|float|string|DateTimeInterface|null $milliseconds = null)
final public jsonSerialize(): mixed
final public serialize(): string
final public toDateTime(): DateTime
final public __toString(): string
final public unserialize(string $data): void
}

更新日志

版本 说明
PECL mongodb 1.12.0 Implements Stringable for PHP 8.0+.
PECL mongodb 1.3.0 Implements MongoDB\BSON\UTCDateTimeInterface.
PECL mongodb 1.2.0 Implements Serializable and JsonSerializable.

目录

add a note add a note

User Contributed Notes 3 notes

up
23
Stephen
6 years ago
When using MongoDB\BSON\UTCDateTime to query be sure for instances of strtotime you are putting the constructor in milliseconds:

$start = new MongoDB\BSON\UTCDateTime(strtotime("midnight") * 1000);
$filter = ['date' => ['$gte' => $start]];
$options = ['sort' => ['date' => -1]];
$query = new MongoDB\Driver\Query($filter,$options);
up
8
mark at DONTSPAM dot moderndeveloperllc dot com
8 years ago
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.
up
1
mike at brenden dot com
6 years ago
SOURCE [url]https://github.com/mongodb/mongo-php-driver/issues/187[/url]

MichaelBrenden --

How do the two Mongo drivers determine if they can translate something into the ISODate format?

Does mongo.so require a class type of MongoDate, or does it determine by data returned by __toString() (i.e., "0.12345678 1234567890")?

Does mongodb.so require a class type of \MongoDB\BSON\UTCDateTime, or does it determine by data returned by __toString() (i.e., "1234567890123")?

jmikola --

>> the ISODate format?

For the record, ISODate is just a helper function in the mongo shell. The actual BSON type is 0x09 in the BSON spec and is best referred to as "UTC date time".
[url]https://docs.mongodb.com/manual/core/shell-types/#return-date[/url]
[url]http://bsonspec.org/spec.html[/url]

>> Does mongodb.so require a class type of \MongoDB\BSON\UTCDateTime, or does it determine by data returned by __toString() (i.e., "1234567890123")?

The driver requires a MongoDB\BSON\UTCDateTime object in order to encode a BSON date type. The relevant instanceof check during BSON encoding is here if you care to see it. The __toString() function exists purely as a convenience for userland applications. The driver does not use it outside of tests.
[url]https://github.com/mongodb/mongo-php-driver/blob/1.3.4/src/bson-encode.c#L215[/url]

The legacy mongo.so driver is similar in that it requires a MongoDate object in order to encode a BSON date type (relevant code is here).
[url]https://github.com/mongodb/mongo-php-driver-legacy/blob/1.6.16/bson.c#L273[/url]
To Top