A Countdown App with AMFPHP and Flash CS4

Posted by David Jumeau
Jul 21 2009

I made my first AMFPHP application with Flash CS4. (Inspired by Lee Brimelow’s AMFPHP tutorial.) It is basically a countdown timer by retrieving the current time from the server. (I made this in preparation for a conference at church, and figured that since we have visits from all over the world, getting the server time via the server [which was in Eastern Time] was the best approach instead of getting the date and time via the Actionscript date class which is client side.)

I found a PHP script by Louai Munajim to retrieve the time left in days; hours; and minutes. And inserted it in the “amfphp/services” folder on my server.

<?php

class CountDown {
    /**
    /* This service creates a countdown provided by parameters
    /* parameters: (year, month, day, hour, minute)
    /* @returns "days;hours;minutes"
    */


    function targetDate($year, $month, $day, $hour, $minute)
    {
        //--------------------------
        // author: Louai Munajim
        // website: www.elouai.com
        //
        // Note:
        // Unix timestamp limitations
        // Date range is from
        // the year 1970 to 2038
        //--------------------------
        //
        // Modified by David Jumeau
        //
        // make a unix timestamp for the given date
      $the_countdown_date = mktime($hour, $minute, 0, $month, $day, $year, -1);
   
      // get current unix timestamp
      $today = time();
   
      $difference = $the_countdown_date - $today;
      if ($difference < 0) $difference = 0;
   
      $days_left = floor($difference/60/60/24);
      $hours_left = floor(($difference - $days_left*60*60*24)/60/60);
      $minutes_left = floor(($difference - $days_left*60*60*24 - $hours_left*60*60)/60);
     
      // OUTPUT
      return $days_left.";".$hours_left.";".$minutes_left;
     
    }
   
}
?>

Then, in my .fla I placed a TextField on the stage with an instance name of: “countdown_txt”, and inserted this code in Flash in the Actions panel:

function onResult(response:Object):void {
trace("response &gt; " + response);

var time_left_array:Array = [];
time_left_array = response.split(";");

if (time_left_array[0] == "0") {
countdown_txt.text = "Today!\n" + ((time_left_array[1] == "0") ? "Right Now!\n" : (time_left_array[1] + " hours\n"));
} else {
countdown_txt.text = time_left_array[0] + " Days : " + time_left_array[1] + " Hrs : " + time_left_array[2] + " Min";
}

}

gw = new NetConnection();
res = new Responder(onResult, onFault);

gw.connect("http://yourserver/amfphp/gateway.php");
gw.call("CountDown.targetDate", res, 2009, 10, 4, 10, 0 ); // Oct 4 2009, 10 am, 0 minutes

Files:

The only issue that I had was that I was getting Flash Errors which prevented me from accessing the CountDown.php function. I found out that it was an internal server error (500) and found at the gotoandlearn forums that the .htaccess file was hindering access to the PHP method in question. Once deleting this file in the “amfphp” directory, amfphp worked fine.

Get Adobe Flash player

DJ

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Trackback URL for this entry