[英]Comparing dates mysql php
我正在尝试制作日历视图。 我需要检查,如果$currentDateTime
是之间startdate
和enddate
在MySQL查询。
我已经尝试过此查询,基于MySQL中的比较日期
SELECT id, startdate, enddate FROM hirings
WHERE startdate <= '$currentDateTime'
AND enddate <= '$currentDateTime'
其中$ currentDateTime的格式为dd-mm-yyyy hh:mm
但它似乎不起作用,我不断得到0 rows
返回。
任何帮助,将不胜感激 :)
确保使用默认的日期时间格式YYYY-MM-DD HH:mm:ss
SELECT id, startdate, enddate
FROM hirings
WHERE '$currentDateTime' between startdate AND enddate
或者如果您想使用当前的SQL时间,那么您甚至不需要查询参数
SELECT id, startdate, enddate
FROM hirings
WHERE NOW() between startdate AND enddate
将查询更改为开始日期> =而不是<=
SELECT id, startdate, enddate FROM hirings WHERE startdate >= '$currentDateTime' AND enddate <= '$currentDateTime'
尝试这个:
SELECT hr.id, hr.startdate, hr.enddate
FROM hirings hr
WHERE STR_TO_DATE('$currentDateTime', '%d-%m-%Y %H:%i:%s')
between hr.startdate AND hr.enddate;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.