Happydevdays Happydevdays - 22 days ago 5
PHP Question

problems converting ISO date in mongo using php

I have the following data in my mongo document:

"last_assigned" : ISODate("2016-11-10T20:34:36.000Z")


When i query the database for documents that include dates like the above, after my attempt to convert it, this is what my var_dump shows:

object(DateTime)[22]
public 'date' => string '1970-01-01 00:00:00.000000' (length=26)
public 'timezone_type' => int 1
public 'timezone' => string '+00:00' (length=6)


And this is what my code looks like - "$value['last_assigned']" is what I get back from the db...

if (!empty($value['last_assigned'])) {
$tempdate = new MongoDate(strtoTime($value['last_assigned']));
var_dump($tempdate->toDateTime());
}


How can i write my code such that it will display the proper value for the date?

EDIT 1

I tried to change the code like this:

$tempdate = (string)$value['last_assigned']->sec;
$tempdate = new MongoDate(strtotime($tempdate));
var_dump($tempdate->toDateTime());


This is what the vardump shows:

object(DateTime)[22]
public 'date' => string '1970-01-01 00:00:00.000000' (length=26)
public 'timezone_type' => int 1
public 'timezone' => string '+00:00' (length=6)

Answer

As per this: http://php.net/manual/en/class.mongodate.php, I had to do something like this:

$tempdate =date('Y-M-d h:i:s', $value['last_assigned']->sec);
$did_details['last_assigned'] = $tempdate;