Today timestamp in php and mongodb

I spent 3 days trying to solve this without success. I am using the MongoDB PHP library and I am trying to convert the timestamp to a valid date using the example in PHP Docs, but it always returns in 1970-01-17.

The code:

$utcdatetime = new MongoDB\BSON\UTCDateTime(1453939200); $datetime = $utcdatetime->toDateTime(); var_dump($datetime); 
+7
php mongodb
source share
2 answers

In the documentation indicated that the constructor takes an integer parameter representing the timestamp in milliseconds, you provide a timestamp in seconds, an incorrect result.

Multiply the value by 1000 to get the timestamp in milliseconds, so return a valid datetime object converted by:

 $timestamp = 1453939200 * 1000; $utcdatetime = new MongoDB\BSON\UTCDateTime($timestamp); $datetime = $utcdatetime->toDateTime(); var_dump($datetime); 
+11
source share

Who needs it: First you must convert the value to a timestamp, after which you can convert it to a valid ISODate. For example:

  $utcdatetime = new MongoDB\BSON\UTCDateTime(strtotime($date)); $date2 = new MongoDB\BSON\Timestamp(1, date($utcdatetime)); 
0
source share

All Articles