phpday 2021 digital edition

The MongoDate class

(PECL mongo >=0.8.1)


A extensão que define essa classe está obsoleta. Alternativamente utilize a extensão MongoDB deve ser utilizada. Alternativas a essa classe incluem:


Represent date objects for the database. This class should be used to save dates to the database and to query for dates. For example:

Exemplo #1 Storing dates with MongoDate


// save a date to the database
$collection->save(array("ts" => new MongoDate()));

$start = new MongoDate(strtotime("2010-01-15 00:00:00"));
$end = new MongoDate(strtotime("2010-01-30 00:00:00"));

// find dates between 1/15/2010 and 1/30/2010
$collection->find(array("ts" => array('$gt' => $start'$lte' => $end)));


MongoDB stores dates as milliseconds past the epoch. This means that dates do not contain timezone information. Timezones must be stored in a separate field if needed. Second, this means that any precision beyond milliseconds will be lost when the document is sent to/from the database.

Sinopse da classe

MongoDate {
/* Fields */
public int $sec ;
public int $usec ;
/* Métodos */
public __construct ( int $sec = time() , int $usec = 0 )
public toDateTime ( ) : DateTime
public __toString ( ) : string


add a note add a note

User Contributed Notes 4 notes

jhonnydcano at yahoo dot com
11 years ago
For showing a human readable MongoDate, you can use something like this:

('Y-M-d h:i:s', $yourDate->sec);
4 years ago
This extension is deprecated, but some people still have to use it.

Please, note that properties (sec, usec) are read-only.

You have to do something like this:


= new \MongoDate();

"\n" . $d->toDateTime()->format(\DateTime::ISO8601);

// move to future for 125 seconds
$d = new \MongoDate($d->sec + 125, $d->usec);

"\n" . $d->toDateTime()->format(\DateTime::ISO8601);

mike at brenden dot com
3 years ago
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")?


>> 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).
richard at securebucket dot com
9 years ago
I wanted a way to check the age of a record.  This code will calculate the time between the creation date of the record, and return the seconds.  I use it for caching pages similar to the max-age header in a browser.  If the record is older, than my limit it deletes itself.

function microtime_diff( $start, $end=NULL ) {
        if( !
$end ) {
$end= microtime();
$start_usec, $start_sec) = explode(" ", $start);
$end_usec, $end_sec) = explode(" ", $end);
$diff_sec= intval($end_sec) - intval($start_sec);
$diff_usec= floatval($end_usec) - floatval($start_usec);
floatval( $diff_sec ) + $diff_usec;

To Top