CakePHP:日期范围

CakePHP:日期范围,cakephp,Cakephp,有没有更好的方法来计算以下内容(我正在尝试从昨天提取所有“操作项”并将其存储在“粘贴到期”中,以及将今天的所有操作项存储在“今天”)-这是在我的“代理”控制器内(代理“hasMany”ActionItem和ActionItem“belongsTo”代理): public function planner(){ $Dayed=日期(“Y-m-D23:59:59”,标准时间(“昨天”); $conditions=array('ActionItem.due'=>$today,'ActionItem.d

有没有更好的方法来计算以下内容(我正在尝试从昨天提取所有“操作项”并将其存储在“粘贴到期”中,以及将今天的所有操作项存储在“今天”)-这是在我的“代理”控制器内(代理“hasMany”ActionItem和ActionItem“belongsTo”代理):

public function planner(){
$Dayed=日期(“Y-m-D23:59:59”,标准时间(“昨天”);

$conditions=array('ActionItem.due'=>$today,'ActionItem.due还有一些改进的空间(尽管正如您所说,当前代码应该可以工作,所以这只是我的一些想法)

首先,如果你只想检查像
00:00:00
23:59:59
这样的时间,那就把时间全部去掉,只需使用
DATE
字段而不是
DATETIME
字段。这样检查起来就容易多了,因为你不必担心时间。(如果时间对于应用程序的其他部分至关重要,则应相应调整下面的示例代码。)

此外,我会使用PHP的函数,而不是
date()
strotime()
,主要是因为每当我处理日期/时间数据时,这几乎是我的一个习惯。这是因为DateTime为您的日期和时间数据添加了很多可能性和灵活性,而不会带来太多麻烦。我可能会选择这样的方式:

public function planner() {

    // Set the DateTime object (defaults to current date/time)
    $today = new DateTime();

    // Overdue actions (everything older than today)
    $overdue = $this->Agent->ActionItem->find('all', array(
        'conditions' => array(
            // Check due against a 'Y-m-d' formatted date of today.
            'ActionItem.due <' => $today->format('Y-m-d'),
            'ActionItem.agent_id' => '1'
        )
    ));

    // Actions due today (or in the future)
    $due = $this->Agent->ActionItem->find('all', array(
        'conditions' => array(
            // Check due against a 'Y-m-d' formatted date of today.
            'ActionItem.due >=' => $today->format('Y-m-d'),
            'ActionItem.agent_id' => '1'
        )
    ));

    // Set the items
    $this->set(compact('overdue', 'due'));
}
public function planner(){
//设置DateTime对象(默认为当前日期/时间)
$today=新日期时间();
//过期的操作(比今天旧的所有操作)
$逾期=$this->Agent->ActionItem->find('all',数组)(
“条件”=>数组(
//根据今天的“Y-m-d”格式日期检查到期日。
'ActionItem.due='=>$today->格式('Y-m-d'),
'ActionItem.agent_id'=>'1'
)
));
//设置项目
$this->set(紧凑(“过期”、“到期”);
}
public function planner() {

    // Set the DateTime object (defaults to current date/time)
    $today = new DateTime();

    // Overdue actions (everything older than today)
    $overdue = $this->Agent->ActionItem->find('all', array(
        'conditions' => array(
            // Check due against a 'Y-m-d' formatted date of today.
            'ActionItem.due <' => $today->format('Y-m-d'),
            'ActionItem.agent_id' => '1'
        )
    ));

    // Actions due today (or in the future)
    $due = $this->Agent->ActionItem->find('all', array(
        'conditions' => array(
            // Check due against a 'Y-m-d' formatted date of today.
            'ActionItem.due >=' => $today->format('Y-m-d'),
            'ActionItem.agent_id' => '1'
        )
    ));

    // Set the items
    $this->set(compact('overdue', 'due'));
}