简体   繁体   English

如何从日期时间列表中找到最近的日期时间?

[英]How to find closest date time from list of date time?

I have this list of time taken from mysql database:我有这个来自 mysql 数据库的时间列表:

2011-07-22 10:12:53 2011-07-22 10:12:53
2011-07-22 10:17:53 2011-07-22 10:17:53
2011-07-22 10:19:42 2011-07-22 10:19:42
2011-07-22 10:29:31 2011-07-22 10:29:31

Right now i want to get the closest date time from the bold date time.现在我想从粗体日期时间中获取最接近的日期时间。 Only the date time after the bold one will be valid, and it must be the closest.只有粗体之后的日期时间有效,并且必须是最接近的。 So the desired result is 2011-07-22 10:19:42所以想要的结果是2011-07-22 10:19:42

How to solve this using php or mysql?如何使用 php 或 mysql 解决这个问题?

Thanks.谢谢。

SELECT ABS(time_to_sec(timediff(col_with_date,'$date'))) AS time_distance FROM table_with_date ORDER BY time_distance LIMIT 1; SELECT ABS(time_to_sec(timediff(col_with_date,'$date'))) AS time_distance FROM table_with_date ORDER BY time_distance LIMIT 1;

   WHERE datefield < '2011-07-22 10:19:42'
ORDER BY datefield DESC
   LIMIT 1

? ?

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

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