我需要使用check_in_range()函数比较日期

时间:2017-06-27 20:20:20

标签: php datetime cakephp-3.0

我需要根据用户选择的日期添加预订时,我的函数add()验证我是否在此日期之前没有预订...我有一个具有字段FROM的预订表(ymd )和HASTA(Ymd)然后我以为我会使用check_in_range函数($ start_date,$ end_date,$ date_to_valuate) 这就是我尝试做的但它阻止了我现在我迷失了这是我的函数add()在我的reservationscontroller.php中

public function add()
{
    $reserva = $this->Reservas->newEntity();
    $res = 'SELECT desde,hasta FROM reservas';
    $arr = $this->Reservas->ExQuery($res);

    if ($this->request->is('post')) {
        $reserva = $this->Reservas->patchEntity($reserva, $this->request->getData());
        if($arr==null){
           // $this->log('keria salvar1', LOG_DEBUG);
        if ($this->Reservas->save($reserva)) {
            //$this->log('salvas con arreglo vacio', LOG_DEBUG);
            $this->Flash->success(__('La habitación ha sido reservada.'));

            return $this->redirect(['controller' => 'reservas', 'action' => 'index']);
        }}
        if($arr!=null){
          for ($i = 0; $i < count($arr); $i++) {
                $start_date = $arr[$i]['desde'];
                $end_date = $arr[$i]['hasta'];
                $fecha_a_evaluar = date("Y-m-d");

                if ($this->check_in_range($start_date, $end_date, $fecha_a_evaluar)) {
                   // $this->log('error fecha', LOG_DEBUG);
                    $this->Flash->error(__('ya existe reserva en esa fecha'));

                } 
                    if ($this->Reservas->save($reserva)) {
                       // $this->log('salvo', LOG_DEBUG);
                        $this->Flash->success(__('La habitación ha sido reservada.'));
                        return $this->redirect(['controller' => 'reservas', 'action' => 'index']);
                    }

                          }}
                          $this->Flash->error(__('La habitación no ha podido ser reservada'));
                         // $this->log('final1', LOG_DEBUG);
                    }
       // $this->Flash->error(__('La habitación no ha podido ser reservada'));
        //$this->log('final2', LOG_DEBUG);


    $habitaciones = $this->Reservas->Habitaciones->find('list', ['limit' => 200]);
    $this->set(compact('reserva', 'habitaciones'));
    $this->set('_serialize', ['reserva']);
}

在我的appcontroler中,这是函数的代码

function check_in_range($start_date, $end_date, $evaluame) {
   $start_ts = strtotime($start_date);
   $end_ts = strtotime($end_date);
   $user_ts = strtotime($evaluame);
   return (($user_ts >= $start_ts) && ($user_ts <= $end_ts) && ($user_ts === $start_ts)); }

我绝望而且坚持

1 个答案:

答案 0 :(得分:0)

由于:

return (($user_ts >= $start_ts) && ($user_ts <= $end_ts) && ($user_ts === $start_ts)); }

check_in_range仅在所有三个参数($start_date, $end_date, $evaluame)相等时返回true