Problem - Flash countdown timer using server time

Posted on 2010-09-14
Last Modified: 2013-11-11
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
Question by:duder78
  • 3
  • 3

Expert Comment

ID: 33677342
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:

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:

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.

Author Comment

ID: 33678278
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?
LVL 39

Expert Comment

ID: 33679084
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?

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.


Accepted Solution

bupper earned 500 total points
ID: 33679264
Ok, here's what I would recommend in this case:

1. Grab the seconds from php since 1970 or whatever using: time(). Don't format yet -- if you do, it won't be easy on the Flash end!!!
2. Send that to Flash through ExternalInterface
3. Within Flash, multiply by 1000 so you have milliseconds (Flash works on miliseconds, PHP on seconds)
4. Use the setInterval function like I describe previously, with a 1 second delay. Increment the time variable by 1000 (1 second) each time the function is called.
5. Set the text fields or other time variables by formatting the milliseconds with the Date class.

Let me know if this would work for you.

Author Comment

ID: 33684685
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 :

Expert Comment

ID: 33684993
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();

Author Closing Comment

ID: 33685198
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!

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question