apache-flexdatemongodbflash-builderzend-amf

flex - zend amf - how to convert to a mongodb compatible date and back?


In flex (flash builder 4) I have a Date() property that I am passing to Zend AMF (php) and I want to store that (as well as other data) in mongoDB.

Mongodb is complaining about the date because it comes across as a Zend_Date object, which appears to be an object containing several properties and Mongodb is saying that it can not accept blank properties.

So, I take that to mean it needs to be converted to something else before I can store it?

If so, how do I do that?

At the moment I have taken the approach of converting the Zend_Date object in to a date string to store it (in php):

$object->readyDT=date("m/d/Y H:i", strtotime($object->readyDT)); //convert to string

That works fine, and I have a string in mongodb of that date.

Now, when I load the date from MongoDB, I need to convert it back to a Zend_Date so it properly converts back in to a Date() object in flex (I have to check that it's not blank or it throws errors in php):

if (isset($object->readyDT) && $object->readyDT!=="") $object->readyDT = new Zend_Date($object->readyDT, 'MM/dd/yyyy hh:mm');

This all works, but it seems like there should be a much easier way to take a Date() from flex and get it stored in MongoDB via Zend AMF/PHP. ?


Solution

  • Sometimes I try to store dates as unix time when I can get away with it. They store easily (just a number), are easy to initialize, and a universal concept in almost every language (including PHP). The only different between AS3 unix time and other unix time's is that AS3s is a bit more precise, milleseconds rather than seconds.

    In a nut shell, unix time is measured in seconds since way before I was born. January 1st, 1970. See: http://en.wikipedia.org/wiki/Unix_time

    reading: http://php.net/manual/en/function.time.php relevant stackoverflow: Getting unix timestamp in milliseconds in PHP5 and Actionscript3