duanbo6482 2014-09-29 02:51
浏览 25
已采纳

如何在mysql / php中一起匹配日期和时间

Date 1 :- 2014-09-27 10:00:00
Date 2 :- 2014-09-29 11:00:00

This range is stored inside the database and it means that user has given time range for 2 days from 27 to 29 and timing from 27 -> 10:00:00 to 29 -> 11:00:00. This means that user will be available from 10 AM to 11 AM between 27 to 29, 2014.

Now if i pass 2014-09-28 13:00:00 which is in date range and also in time range because user specified the entire day for it as can be seen in the range.

SELECT * FROM TABLE_NAME WHERE Id = $Id AND DATE('$currentDate') BETWEEN DATE(From_DateTime) AND DATE(To_DateTime) AND TIME('$timeHour') BETWEEN TIME(From_DateTime) AND TIME(To_DateTime)

From_DateTime = Date1
To_DateTime = Date2
currentDate = 2014-09-28
timeHour = 13:00:00

Now the problem is that logically the parameter passed are within the range but using the query its not because in TIME its not checking the date, 13 is not between 10 & 11 so its not working. I have tried the DATETIME as well but its not working as giving me error.

I need a way to match date & time both at the same time. Anyone having any suggestion. I am using PHP as programming language.

  • 写回答

2条回答 默认 最新

  • ds42774 2014-09-29 03:03
    关注

    If the values are stored in DATETIME like this format you mentioned

    Date 1 :- 2014-09-27 10:00:00
    Date 2 :- 2014-09-29 11:00:00
    

    Then you don't even need all that complexity. Just use normal comparison operators

    SELECT * FROM yourTABLE
    WHERE startDate >= '2014-09-27 13:00:00'
    AND endDate <= '2014-09-29 10:00:00'
    

    Ofcourse you can use your PHP variables instead of the test dates I have there in the query. You can format your PHP values to be in line with MySQL date time format.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表