One of the frustrating things about AMF is that there is no mapping to the Flex Date class from PHP. Unlike converting to an ArrayCollection which utilizes the flex.messaging.io.ArrayCollection class in PHP to automatically map everything for you, there appears to be no support for Date serialization.  So, a few months back, I figured out a relatively easy way to do the conversion on the Flex end without much trouble.  This method involves receiving either UTC Date timestamps (MM/DD/YYYY LL:NN:SS A“), or UNIX timestamps (# of millis since 1970) and converting them to a Date in the setter function for your property.

The first issue involves creating a getter/setter on your Flex end which can “set” values of any kind, but at the same time can “get” values of type: Date.  We are looking for something like this…

?View Code ACTIONSCRIPT
1
2
3
4
5
6
7
8
9
10
11
private var _myDate:Date;
 
public function get myDate():Date
{
    return _myDate;
}
 
public function set myDate(value:*):void
{
    // Convert to Date Class
}

The reason we need a type agnostic setter is because we may not know the type of data we are receiving (String, Number, etc), and since the getter is typed as Date, the Flex compiler will throw an error, since the setter’s parameter is typed differently from the return type on the getter.  The only exception is if one of the values is untyped (*), which is what I did here.

The next step is converting the data we receive in the setter to an actual Date object that we can use in our application.  I wrote a helper class to facilitate this transition.

?View Code ACTIONSCRIPT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class AMFDateConverter
{
 
    public static function convert(value:*):Date
    {
        if (value is Number)
        {
            // The backend value is based in milliseconds, which is PHP's
            // Unix timestamp multiplied by 1000.
            return new Date(value as Number);
        }
        else if (value is String)
        {
            var millis:Number = parseInt(value);
 
            // Are milliseconds typed as a String?
            if (isNaN(millis))
            {
                // The date string is empty so return null
                if (value == "")
                {
                    return null;
                }
 
                // Check to see if it is UTC format...
                var df:DateFormatter = new DateFormatter();
                df.formatString = "MM/DD/YYYY LL:NN:SS A";
 
                return new Date(df.format(value));
            }
            else
            {
                return new Date(millis);
            }
        }
        else if (value is Date)
        {
            // If it is a Date, just return it (for internal use).
            return value as Date;
        }
        else
        {
            return null;
        }
    }
 
}

Now our getters/setters will look like this…

?View Code ACTIONSCRIPT
1
2
3
4
5
6
7
8
9
10
11
private var _myDate:Date;
 
public function get myDate():Date
{
    return _myDate;
}
 
public function set myDate(value:*):void
{
    _myDate = AMFDateConverter.convert(value);
}

Hope this helps.  Any other ideas or improvements to this code are welcomed…