Archive for April, 2009

28
Apr

AS3 Date Class, PHP and Unix Timestamps

   Posted by: Haelix    in Blog

This is a real quick post about Unix Timestamps, PHP, and ActionScript 3’s Date class.  When creating a new Date instance, you can pass in a Unix Timestamp into the Date constructor.


var myDate:Date = new Date( unix_timestamp_from_php );
trace( myDate.toString() );  // Invalid Date

When I was working with this, I continually received Invalid date in my output window.  Looking into the matter, if you read php.net’s time() function description and then taking a look at livedocs Date class we’ll quickly see the problem.

From php.net’s time() description:

time

(PHP 4, PHP 5)

time — Return current Unix timestamp

Description

int time ( void )

Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

 

And then from livedoc’s Date() Class:

Date() Constructor    

Constructs a new Date object that holds the specified date and time.

The Date() constructor takes up to seven parameters (year, month, …, millisecond) to specify a date and time to the millisecond. The date that the newly constructed Date object contains depends on the number, and data type, of arguments passed.

 

So PHP time() gives you a Unix Timestamp for the current time in Seconds, and AS3 wants a time in MiliSeconds.

Here’s the fix:


var myDate:Date = new Date( unix_timestamp_from_php * 1000 );
trace( myDate.toString() ); // Tue Apr 28 13:09:32 GMT-0400 2009

OR You could also simply typecast “unix_timestamp_from_php” to type Number() like so:


var myDate:Date = new Date( Number( unix_timestamp_from_php ) );
trace( myDate.toString() ); // Tue Apr 28 13:09:32 GMT-0400 2009

Both of these work, I’m not 100% sure what happens in the Date constructor, but reading further in the Date livedocs, two bulletpoints stand out as to Why sending a String and a Number value give different results:

  • If you pass one argument of data type Number, the Date object is assigned a time value based on the number of milliseconds since January 1, 1970 0:00:000 GMT, as specified by the lone argument.
  • If you pass one argument of data type String, and the string contains a valid date, the Date object contains a time value based on that date.

I’m currently working on a 2D RPG Game Engine ( think Zelda or FinalFantasyII/IV ) in AS3. At the start of this project, as typical projects with friends go, it seems like a completely easy task. “Hey you know AS3… can you write us a quick game engine for this idea we have?” Sure… I’ve finished a simple little game before, I can totally do that.

Take 1:

From previous (limited) game dev experience I know I’ll need to set up a heirarchy of classes to handle Player Characters, Enemies, Scenes, Buttons, etc. So, thinking things out logically (read: noobishly (?) ) the first thing the team will want to see is going to be a the big-picture, visual framework. I sit down and start hammering out a SceneManager to handle scene changes easily. I’ve created an IZFScene interface to standardize the methods of all of my Scene objects for the SceneManager to manipulate. I’ve got my placeholder backgrounds that I tossed into the Flash Library from photoshop ( or MSPaint ) and I’m off and running creating smooth fade-in/fade-out transitions between scenes. The engine process looks like:

Read the rest of this entry »

Tags: ,