在日期范围内查找

时间:2013-05-14 08:50:43

标签: cakephp date-range cakephp-2.3

我正在尝试在每月日期范围内为每位客户查找所有服务。 按照这个回答的问题CakePHP: Date Range,我在摘要控制器中创建了这段代码:

      public function view($id=null) {
            //other code
            $this->loadModel('Event');
            $summary = $this->Summary->find('first', array('conditions' => array('Summary.id' => $id)));
            $first = date('m-01-Y', strtotime($summary['Summary']['date']));

            //added this for debugging and the value is the date i want
            $this->set('first', $first);

            $last = date('m-t-Y', strtotime($summary['Summary']['date']));

            //debugging, this works too
            $this->set('last', $last);

            $conditions = array("'Event.start' >=" => $first, "'Event.start' <=" => $last,
                                'Event.customer_id' => 'Summary.customer_id'
                               );
            $this->set('events', $this->Event->find('all', array('conditions' => $conditions)));

但是我视图中的$ events变量是空的。我还试图消除customer_id条件来测试日期范围,问题似乎是'Event.start'无法正确比较。和date()函数不能在引号内使用。任何提示?

1 个答案:

答案 0 :(得分:2)

您要为查询添加更多单引号

请将以下内容更改为$ conditions

 $conditions = array("Event.start >=" => "'".$first."'", "Event.start <=" => "'".$last."'",
                                'Event.customer_id' => 'Summary.customer_id'
                               );

如果我可以帮助你,请告诉我

$summary['Summary']['date'] = '4-5-2013'; // where format is 'd-m-Y';

$first = date('m-01-Y H:i:s', strtotime($summary['Summary']['date']));

echo "first".$first;
echo "<br>date is -> ". date('m-t-Y');

输出

first05-01-2013 00:00:00
date is -> 05-31-2013

所以你可以比较日期