1

我试图在每月的日期范围内为每个客户查找所有服务。按照这个回答的问题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() 函数不能在引号内使用。任何提示?

4

1 回答 1

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

所以你可以比较日期

于 2013-05-14T08:59:38.397 回答