[英]php mysql select date from 1 day ago, upto the minute
I want to capture data that took place at the same hour and minute 24 hours ago.我想捕获 24 小时前同一小时和分钟发生的数据。 the formula below does not work.
下面的公式不起作用。 what could be the truth?
真相是什么? I will draw the date range in a single line, not listing.
我将在一行中绘制日期范围,而不是列出。
$now=time();
$24hrb=$now-(24*60*60);
$que = $connc->query("select * from table where datetime='$24hrb';")
In my opinion it would be best to do this all in SQL.在我看来,最好在 SQL 中完成这一切。 You can use the mysql
interval
s options to reduce by 1 day then like
and substr
to trim the seconds off and only pull the datetime through the minute value.您可以使用 mysql
interval
s 选项减少 1 天,然后使用like
和substr
来修剪秒数,仅通过分钟值拉动日期时间。
select * from table where datetime like concat(substr((now() - interval 1 day), 1, 16), '%')
The substr
is taking from a string like this: substr
取自这样的字符串:
2021/01/22 11:12:21
^ ^
and concat
throws the wildcard on the end so anything after that matches.并且
concat
在最后抛出通配符,因此之后的任何内容都匹配。 So your like
is processed as:所以你的
like
被处理为:
2021/01/22 11:12%
$24hrb=time() - (24*60*60);
This is a Timestamp value.这是一个时间戳值。 But you state in comments:
但是你 state 在评论中:
yes.
是的。 datetime format Ymd H:i:s
日期时间格式 Ymd H:i:s
So what you actually should be doing if you insist on using this method is converting your datetime
to a Unix timestamp :因此,如果您坚持使用此方法,您实际上应该做的是将您的
datetime
时间转换为Unix 时间戳:
$que = $connc->query("SELECT * FROM table
WHERE UNIX_TIMESTAMP(`datetime_column`) = '".(int)$24hrb."'");
This is much better and easier to read than messing around with concatenations and string manipulations.这比搞乱连接和字符串操作要好得多,也更容易阅读。
You can extend this action using the MySQL BETWEEN
keyword so that you can get the values found between the range of times, say +/- 30 seconds.您可以使用MySQL
BETWEEN
关键字扩展此操作,以便您可以获得在时间范围之间找到的值,例如 +/- 30 秒。 so:所以:
$que = $connc->query("SELECT * FROM table
WHERE UNIX_TIMESTAMP(`datetime_column`)
BETWEEN ".($24hrb - 30)." AND ".($24hrb + 30));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.