Solved

Problem - Flash countdown timer using server time

Posted on 2010-09-14
7
691 Views
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
0
Comment
Question by:duder78
  • 3
  • 3
7 Comments
 
LVL 5

Expert Comment

by:bupper
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:

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);
0
 

Author Comment

by:duder78
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?
0
 
LVL 39

Expert Comment

by:blue-genie
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?

0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 5

Accepted Solution

by:
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.
0
 

Author Comment

by:duder78
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 :

http://www.grantandrew.com/testing/flash/countdownTimer/countdownTimer2.zip
0
 
LVL 5

Expert Comment

by:bupper
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();
now.setTime(int_milli);
0
 

Author Closing Comment

by:duder78
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!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
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 …

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now