Prgmr Bill Thu May 12 15:01:46 -0400 2011

Subject: Formatting Datetime Columns

Hi, I'd like to see a simple example of how to set the default date format (preferably per model) but I wouldn't mind seeing an example of getting it to work anywhere.

The following code seems to have no effect whatsoever, but it doesn't throw any errors either. I get the date in this format, regardless: "2011-05-12T00:00:00-0400". What I want is "2011-05-12."

$hostsite = self::find(array('conditions' => array('id = ?', $id)));
$hostsite->activation_start_date->format('Y-m-d');
$hostsite->activation_end_date->format('Y-m-d');
$objHostsite = json_decode($hostsite->to_json());
?>

What am I doing wrong here?


Yoan B Fri May 13 04:25:10 -0400 2011

Try this:

1 JsonSerializer::$DATETIME_FORMAT = 'Y-m-d';

ref.: http://www.phpactiverecord.org/docs/ActiveRecord/Serialization#var$DATETIME_FORMAT

BTW:

1 //self::find(array('conditions' => array('id = ?', $id)));
2 //self::find(array('conditions' => array('id' => $id)));
3 //self::find(array('conditions' => compact('id')));
4 //self::find_by_pk($id);
5 self::find($id);
Prgmr Bill Fri May 13 11:41:48 -0400 2011

Could I see an example of where this is intended to be used?

Yoan B Sat May 14 05:39:47 -0400 2011

Where, what?

Yoan B Sun May 15 05:06:50 -0400 2011
Ray Lim Tue Apr 30 17:19:59 -0400 2013

Just wanted to add what worked for me:

ActiveRecord\Config::initialize(function($cfg) {
$cfg->set_model_directory(ACTIVERECORD_DIR);
$cfg->set_connections(array(
'development' => 'mysql://'.DB_USER.':'.DB_PASSWORD.'@'.DB_HOST.'/'.DB_NAME));
});
ActiveRecord\DateTime::$DEFAULT_FORMAT = 'Y-m-d';

(1-5/5)