In my opinion, ActionScript has the worst datatype for dates. They may be a joy to man handle in Flex, but they are not very compatible with any other program.
Our Postgres VO (types) uses timestamps and Flex VO has dates. Here’s what you need to do to make that work back and forth.
We have a handy dandy utility class in Flex. Here’s the code -
package utility { //Used by VO objects that have dates coming into program from PHP import mx.formatters.DateFormatter; public class AMFDateConverter { public static function convert(value:*):Date{ var newDate:Date = new Date( Number(value.slice(0,4)), (Number(value.slice(5,7)) - 1), Number(value.slice(8,10)), Number(value.slice(11,13)), Number(value.slice(14,16)), Number(value.slice(17,19))); return newDate; } } }
Here’s the VO mapping in Flex. I’m including the Boolean Converter, just in case you missed that in a prior blog.
package valueObjects { import utility.BooleanConverter; import utility.AMFDateConverter; [Bindable] [RemoteClass(alias="valueObjects.PeopleVO")] public class PeopleVO { public var pk_id:uint; public var p_first_name:String; public var p_last_name:String; private var _p_birthday:Date; private var _p_active:Boolean; public function get p_birthday():Date { return _p_birthday; } public function set p_birthday(value:*):void { if(value is String) { _p_birthday = AMFDateConverter.convert(value); }else { _p_birthday = value as Date; } } public function get p_active():Boolean { return _p_active; } public function set p_active(value:*):void { _p_active = BooleanConverter.convertBool(value); } } }
So that takes care of your date coming in. Once Flex has it, there are some great manipulation tools in Actionscript. Now when you need to send it back to Postgres, you’ll get PHP to do the work.
Here’s the PHP VO class.
<?php class PeopleVO { public $pk_id; public $p_first_name; public $p_last_name; public $p_birthday; public $p_active; } ?>
Here’s the PHP function.
function dateConversion($input){ if($input == ''){ $output = 'NULL'; }else{ $output = "'".$input."'"; } return $output; }
Here’s the PHP code used to send the data to Postgres. Postgres happily accepts it.
public function savePeople($record) { global $db; $record->p_active = boolConversion($record->p_active); $record->p_birthday = dateConversion($record->p_birthday); $sql = "SELECT * FROM people_save(( '$record->pk_id', '$record->p_first_name', '$record->p_last_name', $record->p_birthday, '$record->p_active'))"; $db->query($sql); return $db->fetchObject('PeopleVO'); }
Please note that the single quotes have been removed ON PURPOSE from the p_birthday. If you send a null date back it looks like this ‘NULL’. Postgres say’s that’s a string and arfs. If you remove the quotes it comes back as NULL and Postgres takes it just fine.

