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 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动