PHP: Convert MongoDate into readable format?

7,854 views
Skip to first unread message

sdotsen

unread,
Feb 23, 2010, 10:05:36 PM2/23/10
to mongodb-user
I insert using new MongoDate(); and a query resulted in the following
"0.42800000 1266979609" (i got this from querying it via PHP on
browser).

How do I convert that into a format I can read (i.e. 2/23/2010
21:46:49 - that's from mongo shell).

sdotsen

unread,
Feb 23, 2010, 10:10:17 PM2/23/10
to mongodb-user
echo date('h:i:s m/d/y','1266979609');

seems to work, what's the "0.42800000" for?

Jon Moss

unread,
Feb 24, 2010, 4:59:23 AM2/24/10
to mongodb-user
Hi,

The other value is believe is microseconds. The MongoDate object
actually has 2 properties ->sec and ->usec

To get a human readable datetime I would do the following:

echo date('h:i:s m/d/y', $aMongoDate->sec);

i.e. just ignore the microseconds part as PHP doesn't handle them that
nicely anyway.

Regards,
Jon

Fitz Agard

unread,
Feb 23, 2010, 11:23:00 PM2/23/10
to mongod...@googlegroups.com
I believe MongoDB returns the values of the time measured in the number of seconds since the Unix Epoch (0:00:00 January 1, 1970 GMT) and the usec is the microseconds part. Doing a var_dump on the object from the cursor shows:

object(MongoDate)#7 (2) {
["sec"]=>
int(1266985090)
["usec"]=>
int(556000)
}

Looks like PHP knows what to do with it if you echo it out.

Armand Golpaygani

unread,
Feb 24, 2010, 4:25:36 AM2/24/10
to mongod...@googlegroups.com
Hello,

A MongoDate object contains two fields : $sec and $usec.
Just feed the php function date() with $sec, eg: echo date('m/d/Y', $yourdate->sec);


Armand

2010/2/24 sdotsen <samna...@gmail.com>
--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


Reply all
Reply to author
Forward
0 new messages