Link to home
Start Free TrialLog in
Avatar of duder78
duder78

asked on

Problem - Flash countdown timer using server time

Hi there, i hadn't seen any good examples of a countdown timer in Flash using the server-side date values so I started looking into it myself but i have run into a bit of a problem.

I'm using actionscript 3 and PHP as my server-side language.  I've managed to get the time into flash by creating a PHP document which grabs the time.  I generated the HTML using PHPs echo function and the date information is then passed to Flash using External interface but i'm not sure how to actually count down this timer once it's loaded.  In the countless examples i've seen, most just keep using the .getTime() function to keep updating the time, but I can't use it because the time is only loaded once on button press within the PHP document i created.

In the Flash document i created, there are a number of textfields.  The ones along the top with the 'day', 'month', 'year', 'hour', 'min', and 'sec' labels are the values as they are passed to Flash using External Interface.  The next one down is the amount of time until christmas of 2010.  The 'now' textfield is the server-side day as it was passed but formatted a bit differently.  Lastly, the 'Xmas' textfield is the date for christmas of 2010.

Anyways, i'll attach the code below, but basically what i need is for this thing to count down visually by the second using this server-side date information which is passed to Flash.  Any help would be much appreciated!

Here is a link to an archive with the 'index.php' and 'countdown.swf' files :
countdown files
Avatar of bupper
bupper
Flag of United States of America image

Ok. There are also ways of getting the time from within Flash, so you don't need to use PHP/ExternalInterface. You can do it with the built in Date class:

http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/Date.html

To make it "count down", you can use an event such as ENTER_FRAME or a function like setInterval. Then, use the Date class to fetch the current time again.

If you don't want to use the Date class, you can increment the time by 1second/framerate (for example, at 20 frames per second, it is 1/20th of a second each time the ENTER_FRAME event is fired). Alternatively, you can use the setInterval function instead of the ENTER_FRAME event (examples below). These methods may work, but can get out of sync over a longer period of time!

here is an example of the ENTER_FRAME event:

addEventListener(Event.ENTER_FRAME,myFunction);
function myFunction(event:Event) {
    // Get and set time again here, using either Date class or timer
}


here is an example of the setInterval function, called every 1 second:

function myFunction():void {
    // Get and set time here.
}
setInterval(myFunction,1000);
Avatar of duder78
duder78

ASKER

Yes this is true, but the reason i'm using External Interface and PHP is to get the time from the server-side.  I've worked in a marketing environment and made a countdown timer using the date class, but I found out that a user can easily change their system time to find out  what happens when the countdown time elapses.

Therefore it's key for this to work with the date as it's grabbed from PHP.  I can't find a straightforward way to count down from this server-time from within Flash.  Is it even possible?
just thinking out loud here,
are you getting the time the swf file is opened from the server and then having flash handle all subsequent calculations to check time till Christmas?
you're hardcoding the day to count till (Christmas 2010) in Flash as well?

ASKER CERTIFIED SOLUTION
Avatar of bupper
bupper
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of duder78

ASKER

Okay, i seem to be making a bit of progress here.  I have the 'Now' time counting down now.  That setIntervail stuff did the trick.  The only thing is....the dates are off now.  Is the fact that the PHP time() in milliseconds is since 1970 throwing things off?  If Flash doesn't do the same thing, from the year 1970 I mean, wouldn't that throw things off?

The now date is completely wrong now but it's counting down.  I suck at math so i'm thinking that the time difference calculations will have to be different now that we're dealing with milliseconds.  Heck I don't know.  Thanks for the help so far, at least the thing is counting down now.

Here's a link to the modified files :

http://www.grantandrew.com/testing/flash/countdownTimer/countdownTimer2.zip
One issue you will run into is that the PHP time is based on the server time zone, NOT the browser time zone... There are some fixes to this, but it is not such an easy thing...

Anyway, I found your issue:
1. Initiate your Date class blank
2. Immediately after, use setTime

like this:
var now:Date = new Date();
now.setTime(int_milli);
Avatar of duder78

ASKER

Thanks a million bupper.  I have everything counting down now and, while I have to check the remaining days for accuracy, everything looks to be working fine.  I finally have this working with the server time! YES!