I have designed a database with a field named creat_time. Its datatype is datetime. And when modifying the beforeSave I used date() function in php to generate data for a field. But when I check it failed (result is 0000-00-00 00:00:00 ). Can anyone tell me that I can used what function to generate this kind of data for datetime type field in MySql (PHP function).
$php_date = strtotime($mysql_date);
$mysql_date = date('Y-m-d H.i:s', $php_date);
or PHP5 way
$dateO = new DateTime($row->create_date);
$sDate = $dateO->format('m/d/y g:i A');
Hi
I created a utility function in my Utils class like this:
[size=2] /**[/size]
* Get DateTime string suitable for database.
*
* @param string $time when already a string, returns the string, when null, returns current time.
* @return string DateTime string suitable for SQL.
*/
static function getDbUtcDateStr($time=null) {
if(!is_int($time)) {
if(is_string($time)) {
return $time;
} else {
$time=time();
}
}
return gmdate('Y-m-d H:i:s',$time);
}
I set all times in the database to UTC and I use
'initSQLs'=>array("set time_zone='+00:00';"), // Make SQL server return DATETIME as UTC
in my db connection declarations (in config.php or equivalent).