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?

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.


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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

830 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