Transfer a Date from Flex to PHP using amfPHP 2.1

Problem

You wish to transfer a Date from your Flex code to a PHP object using a amfPHP. How do you do that?

Solution

amfPHP will transfer the date from Flex to PHP as a time stamp, however this will be in milliseconds (not seconds) so there will be some manipulation required on the PHP side. Also, the date will be transferred in Greenwich Mean Time so some changes might need to be done there as well.

VO object (named DataLine). This will be in the VO directory of your AMF installation. Please note that the class is called DataLine, and the file must be named DataLine.php.

class DataLine
{
public $some_id;   
public $some_int; 
public $some_char;
public $some_date;
};

Service Object (named Bravo). Here I have defined a method insert_date which I'll connect to via amfPHP. The class name is called Bravo, and the file must be name Bravo.php. You will need to change the parameters for the database connection to match your settings.

require_once 'VO/DataLine.php';
 
 
class Bravo
{
private $dbc;
 
 
public function __construct()
{
$this->dbc = new PDO('mysql:dbname=DATABASE_NAME;host=localhost', 
                     'DB_USERNAME', 'DB_PASSWORD');
}
 
 
public function insert_date($p_data)
{
//-----
//--prepare the insert query
//-----
$query = "INSERT INTO table1 VALUES (NULL, :i_int, :i_char, :i_date)";
 
$insert_stmt = $this->dbc->prepare($query);
//-----
 
//-----
//--prepare the date parameter
//-----
if(!empty($p_data->some_date))
  {  
  //-- Remember we get the time stamp from Flex in milliseconds, 
  //-- so we need to divide by 1000 so that we have seconds that 
  //-- we can use with PHP.
  $date = $p_data->some_date->timeStamp / 1000;
  $insert_date = DateTime::createFromFormat('U', $date); 
 
  //-- The date that we get from Flex is in GMT, 
  //-- and I want it converted to local time
  $insert_date->setTimezone(new DateTimeZone('Australia/Sydney'));
 
  //-- create a $myDate variable that is compatible 
  //-- with MySQL for insertion.
  $myDate = $insert_date->format('YmdHis');
  }
else
  {
  $myDate = date('Ymd');
  }
//-----
 
 
//-----
//--bind the parameters, execute the insert statement
//-----
$insert_stmt->bindParam('i_char', $p_data->some_char, PDO::PARAM_STR);
$insert_stmt->bindParam('i_int',  $p_data->some_int,  PDO::PARAM_INT);
$insert_stmt->bindParam('i_date', $myDate, PDO::PARAM_INT);
 
$insert_stmt->execute();
}
 
};

MySQL can support a number of literal formats that will be recognized as dates. Check the manual for supported date and time literals for further reference.

Notice in the PHP code that I am referencing $p_data->some_date->timeStamp. If you're wondering where the heck that came from, it came from amfPHP. You can check it out in the file Date.php under the amf directory Amfphp\Core\Amf\Types.

Database table

CREATE TABLE IF NOT EXISTS `table1` (
  `table1_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `some_int` int(10) unsigned DEFAULT NULL,
  `some_char` varchar(255) DEFAULT NULL,
  `some_date` datetime DEFAULT NULL,
  PRIMARY KEY (`table1_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 
COMMENT='A test table used for test applications with Flex' 
AUTO_INCREMENT=1 ;

Flex Code

DataLineFlex Value Object. Visit some of my other tutorials for further explanations on VO's.

package VO
{

/* must have the remoteClass directive otherwise the line:
... event.result as DataLineFlex;
will result in an error.
*/
[RemoteClass(alias="DataLine")]
[Bindable]
public class DataLineFlex
{
public var some_id : int;
private var _some_int : int;
public var some_char : String;
public var some_date : Date;

public function DataLineFlex()
{

}

}
}

Main MXML file. I have had to remove a forward slash from the fx value in the Application tag so that the code would display properly. Keep this in mind if you are going to copy and paste. I have also added some comments about Date formats that Flex will recognize. Another tutorial discusses the attributes of the RemoteObject and what they mean.



xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955" minHeight="600">


import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.rpc.events.ResultEvent;

private var myDataLine : DataLineFlex;

protected function button1_clickHandler(event:MouseEvent):void
{
//The following list indicates some of the valid formats for
//creating a Date:
/*
Day Month Date Hours:Minutes:Seconds GMT Year (for instance, "Tue Feb 1 00:00:00 GMT-0800 2005", which matches toString())
Day Month Date Year Hours:Minutes:Seconds AM/PM (for instance, "Tue Feb 1 2005 12:00:00 AM", which matches toLocaleString())
Day Month Date Year (for instance, "Tue Feb 1 2005", which matches toDateString())
Month/Day/Year (for instance, "02/01/2005")
Month/Year (for instance, "02/2005")
*/

var trial : DataLineFlex = new DataLineFlex();
trial.some_char = 'trial of AMFPHP';
trial.some_int = 9;
trial.some_date = new Date("Wed Apr 25 2012 4:00:00 PM");

Bravo.insert_date(trial);
}

]]>


source="Bravo"
destination="amfphp">



When I click on the Test Button, nothing happens on the screen, however when I have a look at the database I can see that the record has indeed been inserted into the table. Also notice that the date and time in the table matches the date and time I created in my AS code. This was only possible because of the modification to the PHP code that converted the DateTime object to the correct time zone.

Tags: 

Comments

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.