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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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?

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …

732 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