在cakePHP查询中更改日期格式

时间:2015-01-24 06:38:44

标签: sql datetime cakephp conditional-statements

我有一个表格room_booking_datas,其中字段checkin_time的格式为datetime。因此,此字段的值为2015-01-24 05:25:012015-01-24 05:25:40等。现在,我需要检查2015-01-24的特定日期的最后一次检入数据。

$date = `2015-01-24`;
$cond['RoomBookingData.checkin_time'] = $date;

$res = $this->RoomBookingData->find('first', array('conditions' => $cond, 'order' => 'RoomBookingData.checkin_time DESC'));

但正如预期的那样,$res显示空结果!如何获得$date的结果?

2 个答案:

答案 0 :(得分:1)

$date = `2015-01-24`;

$res = $this->RoomBookingData->find(
'first', 
array(
    'conditions' => array( 
        DATE('RoomBookingData.checkin_time') => $date
    ), 
    'order' => 'RoomBookingData.checkin_time DESC'
    )
);

答案 1 :(得分:0)

$date = '2015-01-24 00:00:00';
$date = str_replace('-', '/', $date);
$tomorrow = date('m-d-Y H:i:s',strtotime($date . "+1 days"));

然后......

$query_array = array(
    'conditions' => array('RoomBookingData.checkin_time <' => $tomorrow),
    'order' => array('RoomBookingData.checkin_time DESC'),
);