In case you’re enthusiastic like me and wanted to use the new MongoDB driver for PHP ASAP, you’ve might have stumbled upon differences in date handling.
Untill now, when you’ve wanted to properly insert dates, you’d construct
new MongoDate and insert it as such. Well, there is no such class any more in the new driver, and to insert a date properly, you need to use
new MongoDB\BSON\UTCDatetime. Notice the difference in constructor arguments, though – the new
UTCDateTime accepts Unix timestamp in milliseconds, not seconds like
Of course, you have the use the new class when querying as well:
$docs = $collection->find([ 'ts' => [ '$gt' =>new MongoDB\BSON\UTCDatetime($unix_timestamp * 1000) ] ]);
Also published on Medium.