Lars Erik Grambo
Lars Erik Grambo

Reputation: 311

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).

Upvotes: 1

Views: 2029

Answers (3)

Lars Erik Grambo
Lars Erik Grambo

Reputation: 311

So to summerize, the answer is a php-script containing only this:

<?php 
header("Content-Type: text/plain");
print time();
?>

and the script I use to get the time from the server is:

//cut from a larger script
{
loader = new URLLoader();
loader.load(new URLRequest("http://grambogames.net/serverTime.php"));
loader.addEventListener(Event.COMPLETE, onServerTimeLoad);
trace("waiting for time to load");
}



private function onServerTimeLoad(e:Event):void
{
trace("date loaded:", loader.data);
try
{
    var num:Number = Number(loader.data) * 1000
    dato = new Date(num);
    trace("tid & dato synkronisert med server.", dato);
}
catch (e:Error)
{
    //the passed date was invalid, lets just use the local system date
    dato = new Date();
    trace("date did not work, local time used");
    txt_error.text = "ERROR! COULD NOT GET TIME FROM SERVER!"
}
    if (! dato)
{
    //your output from the server isn't formatted in a way flash can convert it
    trace("not working, date farmated wrong");
}
}

thanks to LondonDrugs_MediaServices

...I just hope no more bugs shows up.

Upvotes: 0

BadFeelingAboutThis
BadFeelingAboutThis

Reputation: 14406

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;
?>

Upvotes: 0

user56725
user56725

Reputation:

You could pass the date to the swf file like this:

<param name=FlashVars value="date=<?php date(); ?>" />

You can read more about how to pass date to the swf file here: http://helpx.adobe.com/flash/kb/pass-variables-swfs-flashvars.html And more about how to format the date here: http://php.net/manual/en/function.date.php

In this variant to have to rename your .html file to .php.

Upvotes: 1

Related Questions