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.

Sınıf Sözdizimi

final class MongoDB\BSON\UTCDateTime implements MongoDB\BSON\UTCDateTimeInterface, MongoDB\BSON\Type, Serializable, JsonSerializable, Stringable {
/* Yöntemler */
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

Sürüm Bilgisi

Sürüm: Açıklama
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

User Contributed Notes 3 notes

5 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);
mark at DONTSPAM dot moderndeveloperllc dot com
7 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:

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

string(17) "1477534060.918415"
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.
mike at brenden dot com
6 years ago
SOURCE [url][/url]

MichaelBrenden --

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

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

Does 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".

>> Does 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.

The legacy driver is similar in that it requires a MongoDate object in order to encode a BSON date type (relevant code is here).
To Top