Sujan
Sujan

Reputation: 135

Query to datetime field with date condition only in cakephp

In cakephp query conditions, where i have only date which have to meet the conditions with datetime field of table. suppose i have date only(2013-09-21) and field is (2013-09-21 07:45:23). i have get problem to give the condition here.

Upvotes: 8

Views: 10616

Answers (2)

karmicdice
karmicdice

Reputation: 1061

$date_string = "2013-09-21";
$new_date = date('Y-m-d H:i:s', strtotime($date_string));
$row = $this->Model->find('all',
                    array('fields' => array('........'),
                    'conditions' => array(
                        'Story.created >' => $new_date)));

Upvotes: 1

AgRizzo
AgRizzo

Reputation: 5271

If @Aryan 's solution doesn't work because you need to convert between formats at the database, then you want to use the MySQL function DATE. This would change the find example to be this:

$row = $this->Model->find('all',
                    array('fields' => array('........'),
                    'conditions' => array(
                        'DATE(Story.created) >' => $date)));

Upvotes: 12

Related Questions