简体   繁体   中英

Find record created this week CakePHP 3

Tried doing this:

$sdate = new \DateTime('last saturday');
$edate = new \DateTime();

$query = $this->Orders->find()->where(["Orders.created BETWEEN '.$sdate.' AND '.$edate.'"]);

even tried using date_diff in php and mySql

$diff = date_diff($sdate,$edate);
$query = $this->Orders->find()->where(['(DATEDIFF(Orders.created, NOW())) >' => $diff]);

Both don't seem to work. Any help here will be terribly appreciated. Thanks

You can use query builder:

$sdate = new \DateTime('last saturday');
$edate = new \DateTime();

$query = $this->Orders->find()
    ->where(function ($exp, $q) use ($sdate, $edate) {
        return $exp->between('created', $sdate, $edate);
    });

More info: Query Builder

$query = $this->Orders->find()->where(["Orders.created > DATE_SUB(DATE(NOW()), INTERVAL 1 WEEK)"]);

试试这个。

I needed something similar, and here's how I did it relatively easily, yet using CakePHP approach (as opposed to using plain SQL):

$query = $this->Orders->find('all', array(
    // some find parameters such as group, order etc. may go here
));
$query->where([
    'Orders.created >=' => $query->func()->date_sub([
        $query->func()->now(),
        'INTERVAL 1 WEEK' => 'literal',
    ]),
]);

Sources:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM