微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何比较cakephp4中的日期时间

如何解决如何比较cakephp4中的日期时间

我正在使用以下代码来比较 DB DateTime 和输入字段 DateTime 是否相等但它不起作用,请帮我解决这个问题-

$appointment_time = $this->Appointments->find()->select(['appointment_time']);
$appointment_time1 = $this->request->getData('appointment_time');
$dt = ($appointment_time == $appointment_time1);

解决方法

始终尝试对变量使用 debug() 函数以了解差异。

debug($appointment_time1);
debug($appointments);

就您而言,$appointment_time 不是您所期望的,而是 \Cake\ORM\Query 对象。而 $appointment_time1 是一个字符串。

尝试:

// convert posted string to date time format
$appointment_time1 = date('Y-m-d H:i:s',strtotime($this->getRequest()->getData('datetime')));

// fetch data from db
$appointments = $this->Appointments->find()->select(['appointment_time']);

foreach ($appointments as $appointment) {
   echo $appointment->appointment_time->format('Y-m-d H:i:s') == $appointment_time1 ? 'OK' : 'NOT OK';
}

$appointment = $this->Appointments->find()->select(['appointment_time'])->first();

echo $appointment->appointment_time->format('Y-m-d H:i:s') == $appointment_time1 ? 'OK' : 'NOT OK';

也试试:

$appointment->appointment_time->getTimestamp() == strtotime($this->getRequest()->getData('datetime'));

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。