CakePHP:日期范围 [英] CakePHP: Date Range

查看:75
本文介绍了CakePHP:日期范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个更好的方法来计算以下(我想从昨天拉所有行动项目,并将其存储在paste_due,所有行动项目今天,并将其存储在今天) - 这在我的代理控制器(代理'hasMany'ActionItem和ActionItem'belongsTo'代理)内部:

Is there a better way for me to calculate the following (I'm trying to pull all "Action Items" from yesterday and store them in "paste_due", and all action items for today and store them in "today") - This is inside of my "Agents" controller (Agent 'hasMany' ActionItem and ActionItem 'belongsTo' Agent):

public function planner() {

    $yesterday = date("Y-m-d 23:59:59", strtotime("yesterday"));
    $conditions = array('ActionItem.due <' => $yesterday, 'ActionItem.agent_id' => '1');
    $this->set('past_due', $this->Agent->ActionItem->find('all', array('conditions' => $conditions)));

    $today = date("Y-m-d 00:00:00", strtotime("today"));
    $today_end = date("Y-m-d 23:59:59", strtotime("today"));
    $conditions = array('ActionItem.due >' => $today, 'ActionItem.due <' => $today_end, 'ActionItem.agent_id' => '1');
    $this->set('today', $this->Agent->ActionItem->find('all', array('conditions' => $conditions)));
}

上面的工作,但我不确定如果是最好的方法。

The above works, but I'm not for sure if it's the best way to go about i..

推荐答案

有一些改进的空间(虽然正如你所说,当前的代码应该工作,所以这只是我的一些想法)。

There would be room for some improvement (though as you say, the current code should work, so it's just some of my thoughts).

首先,如果你只是检查时间,如 00:00:00 23:59:59 ,完全放弃时间,只需使用 DATE 而不是 DATETIME 字段。它使检查更容易,因为你不必担心时间。 (如果时间对于你的应用程序的其他部分是必要的,那么下面的示例代码应该相应地调整。)

First of all, if you're only going to check against times like 00:00:00 and 23:59:59, drop the times altogether and just use a DATE field rather than a DATETIME field. It makes checking much easier as you don't have to worry about times. (If the times are essential for other parts of your application, then the below example code should be adjusted accordingly.)

此外,我将使用 DateTime PHP的功能,而不是 date()与 strtotime(),主要是因为它每天处理日期/时间数据时都是我的习惯。这是因为DateTime为您的日期和时间数据添加了很多可能性和灵活性,而没有太多的麻烦。这样的东西是我可能去的:

Furthermore I'd use the DateTime functions of PHP rather than the date() with strtotime(), mainly because it's pretty much a habit of mine whenever I work with date/time data. This is because DateTime adds a lot of possibilities and flexibility to your date and time data without too much hassle. Something like this is what I'd probably go for:

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'));
}

这篇关于CakePHP:日期范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆