Using Flash’s Date Class in AMF PHP to MySQL

So you’re trying to rectify your dates, eh? Here’s a little funbit. If you want to take a Flash Date, transfer it to AMF, convert it to a PHP date, then insert that into a MySQL database as a DATETIME, you’re in for one hell of a headache. How does flash store it’s date class anyway? How can PHP read it? What about that whole milliseconds to seconds thing? No worries, my friends, I have a solutions for you.

Actionscript:

var d:Date = new Date()

This creates a new date. This can then be passed to AMF using whatever means you feel work best. I’m using a codeigniter port with AMF as the model that was built by my pal, Steve.

PHP to make a DATE in MySQL:
I have passed in an FP object that contains my Date object.

$date = $fp['d']/1000;
$date = date("Y-m-d","$date");

This will output your date as something like 2010-10-18. You can modify the output to be anything you like, such as:
How does it work? Flash automatically encodes Dates as the number of milliseconds since the Unix Epoch. This means that to convert it to the typical date structure in PHP, you have to convert it to seconds, which involves division by 1000. From there we can turn it into a date, but it has to be turned into a string, hence the quotes.

$date = $fp['d']/1000;
$date = date("Y-m-d H:i:s","$date");

This would give you something like 2010-10-18 15:45:30
This number can be used to insert into or compare against MySQL DATETIME entries.

Leave a Reply

Your email address will not be published. Required fields are marked *