简体   繁体   English

日期范围时间查询

[英]Date range time query

I'm trying to query MySQL using a start and end date range. 我正在尝试使用开始和结束日期范围查询MySQL。 At the moment my variables are: 目前,我的变量是:

$begin = $_POST['begin'];
$begin = date('Y/m/d H:i:s', strtotime($begin));
$end = $_POST['end'];
$end = date('Y/m/d H:i:s',strtotime($end));

strtotime($begin) produces an argeeable timestamp '00:00:00'. strtotime($begin)产生可协商的时间戳“ 00:00:00”。 However I'd like strtotime($end) to output '11:59:59' so that users can query a particular day using the date range in my form. 但是我希望strtotime($end)输出'11:59:59',以便用户可以使用表单中的日期范围查询特定的一天。 Can someone kindly demonstrate how to set to time as '23:59:59', bearing in mind I wish to use the $end date variable from my form? 记住我希望使用表单中的$end date变量,有人可以好心地演示如何将时间设置为“ 23:59:59”吗?

My query from PHP is: 我从PHP的查询是:

"SELECT OrderId, Qty, Del, Timestamp FROM Orders WHERE Timestamp BETWEEN '$begin' AND '$end'"; “从“ $ begin”和“ $ end”之间的时间戳中选择订单中的OrderId,Qty,Del,时间戳。”;

Many thanks. 非常感谢。

strtotime simply produces an integer timestamp, so you could do strtotime只会产生整数时间戳,因此您可以执行

$end = date('Y/m/d H:i:s', strtotime($end) + 86400 - 1);

add a day to get "tomorrow", then remove one second to get the moment before midnight of the day you started on. 添加一天以获取“明天”,然后删除一秒钟以获取开始一天的午夜之前的时刻。

Or simpler yet: 或更简单:

$end = date('Y/m/d', strtotime($end)) . ' 11:59:59';

produce the appropriate date, then tack on the time portion manually yourself. 生成适当的日期,然后自己手动添加时间部分。

如果您想对时间进行硬编码,只需将其连接:

$end = date('Y/m/d', strtotime($end)) . ' 11:59:59';

strtotime returns a timestamp. strtotime返回时间戳。 you can add 86399 (1 day - 1 second) 您可以添加86399(1天-1秒)

or you can use the strtotime + syntax so strtotime($end . "+ 23 hours") 或者您可以使用strtotime +语法,这样strtotime($end . "+ 23 hours")

or if there is no time info in the variable you can just add it yourself strtotime($end . "+ 23:59") 或者,如果变量中没有时间信息,则可以自己将其添加到strtotime($end . "+ 23:59")

your other option is to convert to a datetime object which gives much more flexibility 您的另一种选择是转换为datetime对象,这提供了更大的灵活性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM