[ Team LiB ] |
Recipe 10.1 Finding the Current Date and Time10.1.1 ProblemYou want to know the current date and time. 10.1.2 SolutionCreate a new date using the Date( ) constructor with no parameters. Alternatively, use a CGI script or any other server-side language to return the server time, and create a new Date object from that value. 10.1.3 DiscussionThe date and time that ActionScript can calculate on its own is based on the client computer's date and time settings. Therefore, if the user's computer has the incorrect time, so will the Flash movie. With that caveat in mind, you can retrieve the current client-side date and time by creating a new Date object using the Date( ) constructor with no parameters, as follows: // Create a new Date object. today = new Date( ); // Displays client-side date and time trace(today);
If an active Internet connection is available, the Flash movie can try to retrieve the date and time from a server. This technique can ensure more accurate dates and times. Although the server's time settings might be inaccurate, at least the time will be consistent for all client movies. The basic process when reading the time from a server is as follows:
PHP is a scripting language that can be found on a large number of web hosts. It is quite simple to create a PHP page to output the current time and date as the number of seconds since the Epoch. All you need to do is create a PHP document with the following content and upload it to the server: now=<?php echo time( );?> If you don't have PHP on your server, or if you are simply more comfortable with Perl (another language that is almost universally available on web servers), then here is a Perl script that outputs the number of seconds since the Epoch: #!/usr/local/bin/perl print "Content-type:text/plain\n\n"; print "now="; print time; There are a few tips to keep in mind when setting up this script on your server:
Within the Flash document, you need to load the time value from the server. The best way to accomplish this is to use a LoadVars object. The LoadVars class was introduced in Flash MX. For compatibility with earlier Flash Player versions, use the MovieClip.loadVariables( ) method instead. If you pass the Date( ) constructor a single value, ActionScript interprets it as the number of milliseconds since the Epoch and creates a new Date object that corresponds to that value. Therefore, you must multiply the value returned by the script (which is in seconds) by 1,000. // Create a LoadVars object. lv = new LoadVars( ); // Use the load( ) method to call the CGI script on the server. If using PHP, this // should point to the appropriate PHP page, such as getDate.php, instead. lv.load("http://www.person13.com/cgi-bin/getDate.cgi"); // onLoad( ) is invoked automatically when the server returns a response. lv.onLoad = function ( ) { // Create a Date object by passing (seconds * 1000) to the Date( ) constructor. The // value returned by the server is this.nameOfVariable, in this case, this.now. // Convert the string value to a number before multiplying it by 1000. var serverDate = new Date(Number(this.now) * 1000); // Display the date and time returned by the server (in this case, with time zone // offsets for the user's computer settings). trace(serverDate); }; The date is always stored in ActionScript as the milliseconds since the Epoch, but it is always displayed with proper offsets based on the user's local time zone setting (unless you specifically use the UTC methods). So if the user's computer has the incorrect time zone setting, the display might be incorrect. However, the actual date (as stored in Epoch milliseconds) is still correct. 10.1.4 See AlsoIf your server's time is not reliable or accurate enough for your needs, there are many existing date and time servers on the Internet from which you can retrieve reasonably accurate date and time information. For example, see http://tycho.usno.navy.mil. For details on synchronizing time via the Network Time Protocol, see http://www.ntp.org and Recipe 10.7. |
[ Team LiB ] |