[英]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.