Q:

Actionscript 3: How do I get date from server (not client)

I am working on a Christmas calendar for my school, when I noticed that, the Date-class is not what I though it was.
It gets the time from the client CPU, and that won't do.
So, I been looking around on the web, but there have been no good answers (at least that I could make any sense of).
So here it is:
How do i get the time and date (*) in UTC +1, from the server my flash-program are hosted, or some external time-server?
*In UNIX-time, or somehow else actionscript 3 can understand
My search for answers had suggestions for some php-scripts, but i don't understand how to use that (there where a few examples, but they where in AS2).

A:

A more secure way of doing this (eg not using flash vars which can be tampered with), would be to create a very simple server page (outputs plain text instead of html) that outputs the date, then load that into flash with a loader and parse it into a native Date object.
The flash side would look like this:
var loader:URLLoader = new URLLoader()
loader.load(new URLRequest("your server page"))
loader.addEventListener(Event.COMPLETE, onLoaded)
function onLoaded(evt:Event){
    var date:Date;
    try{
        date = new Date(Number(loader.data)); //need to cast the loader data as a number
    }catch(e:Error){
    }

    if(!date){
        //your output from the server isn't formatted in a way flash can convert it
        date = new Date(); //use local time
    }
}

The reason for using try/catch, is because if output from the server isn't a valid date it will throw an error when trying to convert it.  If an error is thrown anywhere inside of a try block, it will exit at the line of the error (skip the rest of the code) and run what is in the catch block.  If there is no error in the try block, the code in the catch block will never run.

With PHP  (though I don't have php anywhere handy to check so this may need to be corrected by someone, and it's been about 10 years since I've programmed with it)
<?php 
    header("Content-Type: text/plain");
    print time() * 1000;
?>

