Skip to content
Advertisement

Printing A MongoDB Date From PHP

How in PHP do I get the regular timestamp format out of a MongoDB date?

Assume I have:

$my_date;
print_r($my_date);

The print_r output is:

MongoDate Object ( [sec] => 1346300336 [usec] => 593000 )

But doing:

echo $my_date;

Outputs:

0.59300000 1346300336

Even tried:

echo (string)$my_date

Same thing.

Advertisement

Answer

$my_date->sec is the unix timestamp, use date() function to show it in required format.

echo date('Y-m-d H:i:s', $my_date->sec);
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement