[英]Find the nearest time from list of array
I am looking for two values in an array: 我正在寻找数组中的两个值:
Here is the array: 这是数组:
$dates = array(
'08/29/2013 08:00',
'08/29/2013 08:10',
'08/29/2013 08:11',
'08/29/2013 12:20',
'08/29/2013 12:21',
'08/29/2013 12:21',
'08/29/2013 17:30',
); // etc.
One of the best ways to compare times is to convert your strings to times using strtotime()
. 比较时间的最佳方法之一是使用
strtotime()
将字符串转换为时间。
// Original array of date strings
$dates = array(
'08/29/2013 08:00',
'08/29/2013 08:10',
'08/29/2013 08:11',
'08/29/2013 12:20',
'08/29/2013 12:21',
'08/29/2013 12:21',
'08/29/2013 17:30',
);
// Make a new array of time stamps based on your strings
$dates_dt = array();
foreach($dates as $date)
$dates_dt[] = strtotime($date);
// Same idea with the dates you want to look at
$date_last1 = strtotime('08/29/2013 08:00');
$date_last2 = strtotime('08/29/2013 13:00');
$date_first1 = strtotime('08/29/2013 12:00');
$date_first2 = strtotime('08/29/2013 17:00');
Here's the magic - some pretty simple functions that return the closest date before (or the first entry), and the closest after (or the last) in your array. 这就是魔术-一些非常简单的函数,它们返回数组中最接近的日期(或第一个条目),以及最接近的日期(或最后一个)。
function dateBefore($date, $dateArray){
$prev = $dateArray[0];
foreach( $dateArray as $d ){
if( $d >= $date )
return date("Y-m-d H:i", $prev);
$prev = $d;
}
}
function dateAfter($date, $dateArray){
foreach( $dateArray as $d ){
if( $d > $date )
return date("Y-m-d H:i", $d);
}
return date("Y-m-d H:i", end($dateArray));
}
echo dateBefore($date_last1, $dates_dt); // Outputs: 2013-08-29 08:00
echo dateBefore($date_last2, $dates_dt); // Outputs: 2013-08-29 12:21
echo dateAfter($date_first1, $dates_dt); // Outputs: 2013-08-29 12:20
echo dateAfter($date_first2, $dates_dt); // Outputs: 2013-08-29 17:30
http://codepad.org/cBYPuowt http://codepad.org/cBYPuowt
Note Probably a good idea to sort the array of times as well so they're for sure in order. 注意对时间数组进行排序可能也是个好主意,因此可以肯定它们是按顺序排列的。
foreach($dates as $date)
$dates_dt[] = strtotime($date);
// Add sort here
sort($dates_dt);
You can convert all of the string timestamps to long values (milliseconds since 1970 epoch). 您可以将所有字符串时间戳转换为长值(自1970年以来的毫秒数)。 Then just calculate the delta for relevant values (greater than 08/29/2013 08:00, for instance) compared to the upper limit (08/29/2013 13:00, for instance).
然后,只需计算与上限(例如,2013年8月29日13:00)相比的相关值(例如,大于08/29/2013 08:00)的增量即可。 The smallest delta is the closest to the upper limit.
最小增量最接近上限。 The greatest delta is furthest.
最大的三角洲最远。
This is how you calculate the seconds since epoch: http://php.net/manual/en/function.strtotime.php 这是您计算自纪元以来的秒数的方法: http : //php.net/manual/en/function.strtotime.php
$nearest = $dates[0];
$now = time();
for ($i = 0; $i < count($dates); $i++) {
$date = $dates[$i];
$timestamp = strtotime($date);
if (abs(strtotime($date) - $now) < abs(strtotime($nearest) - $now))
$nearest = $date;
}
echo $nearest;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.