How to use mysql now () function in cakephp for date fields?

I have this code in my cakephp project:

$this->data['MyObject']['expire_date'] = 'NOW()';

and...

$this->MyObject->save($this->data);

But this will not save the data in the database. After googling, I found that I can use now () like this. But it seems wrong, what's the right way to use it?

PS: My MySQL field is of type "DATE". It works if I use like this:

$this->data['MyObject']['expire_date'] = date( 'Y-m-d', mktime(0, 0, 0, date("m"), date("d")+30,   date("Y")));
+3
source share
5 answers
$this->data['MyObject']['expire_date'] = DboSource::expression('NOW()');
+17
source

For cakephp 2.x users:

$db = $this->MyObject->getDataSource(); 


$this->data['MyObject']['expire_date'] = $db->expression('NOW()');
+9
source

$this->data['MyObject']['expire_date'] = date('Y-m-d H:i:s'); 

date() , "timestamp".

DboSource::expression(), , .

+6

For CakePHP 3 (in case someone is looking), you will need to use the SQL functions:

$query = $this->Example->query();
$query
    ->insert(['created','id'])
    ->values(array(
        'created' => $query->func()->now(),
        'id' => $id,
    ));
$result = $query->execute();

Sources:

0
source
$this->data['MyObject']['expire_date'] = strtotime('now');
-3
source

All Articles