比较日期mysql php

时间:2014-06-25 19:42:58

标签: php mysql comparison

我正在尝试制作日历视图。我需要检查$currentDateTime是否在mysql查询中的startdateenddate之间。

我已根据Compare dates in MySQL

尝试了此查询
SELECT id, startdate, enddate FROM hirings
WHERE startdate <= '$currentDateTime'
AND enddate <= '$currentDateTime'

其中$ currentDateTime的格式为dd-mm-yyyy hh:mm

但它似乎不起作用,我不断返回0 rows

任何帮助将不胜感激:)

3 个答案:

答案 0 :(得分:1)

确保使用默认日期时间格式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

答案 1 :(得分:0)

将查询更改为startdate&gt; =而不是&lt; =

SELECT id, startdate, enddate FROM hirings WHERE startdate >= '$currentDateTime' AND enddate <= '$currentDateTime'

答案 2 :(得分:0)

试试这个:

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;
相关问题