How to Get Closest Date Compared to an Array of Dates in PHP

How to get closest date compared to an array of dates in PHP

I may not have the best naming conventions, but here goes.

I calculate the intervals between the array of dates and the given date. I then do a sort, to find the "smallest" difference.

$dates = array
(
'0'=> "2013-02-18 05:14:54",
'1'=> "2013-02-12 01:44:03",
'2'=> "2013-02-05 16:25:07",
'3'=> "2013-01-29 02:00:15",
'4'=> "2013-01-27 18:33:45"
);

function find_closest($array, $date)
{
//$count = 0;
foreach($array as $day)
{
//$interval[$count] = abs(strtotime($date) - strtotime($day));
$interval[] = abs(strtotime($date) - strtotime($day));
//$count++;
}

asort($interval);
$closest = key($interval);

echo $array[$closest];
}

find_closest($dates, "2013-02-18 05:14:55");

find the closest next date from the array in php

This is the solution I used for finding the closest next date for my program and it's working.

   $date = '02/21/2019';
$allDates= array
(
'2019-02-17',
'02/09/2019',
'02/23/2019',
'02/18/2019',
'02/25/2019',
'03/04/2019',
'03/11/2019',
'03/18/2019',
'03/25/2019',
'04/01/2019',
'04/08/2019',
);
function date_sort($a, $b) {
return strtotime($a) - strtotime($b);
}
usort($allDates, "date_sort");
foreach ($allDates as $count => $dateSingle) {
if (strtotime($date) < strtotime($dateSingle)) {
$nextDate = date('m-d', strtotime($dateSingle));
break;
}
}
echo $nextDate;

How to sort dates from closest date to the furthest in an array of dates in PHP

You need to compare the differences of the date_res entries to today:

usort($arr, function($dateA, $dateB) use ($today) {
return
abs(strtotime($dateA['date_res']) - strtotime($today)) -
abs(strtotime($dateB['date_res']) - strtotime($today));
});

The abs is used to get the "distance" between date_res and today regardless if its before or after. Then you just compare the "distances".

Working example.

References

  • usort
  • abs

Find the previous and next nearest date from a array of date

Try this,

$date_array = array
(
'2015-09-01 12:00:00',
'2015-12-01 12:00:00',
'2016-03-01 12:00:00',
'2016-06-01 12:00:00',
'2016-09-01 12:00:00',
'2016-12-01 12:00:00',
'2017-03-01 12:00:00',
'2017-06-01 12:00:00',
'2017-09-01 12:00:00',
'2017-12-01 12:00:00',
'2018-03-01 12:00:00',
'2018-06-01 12:00:00',
'2018-09-01 12:00:00',
'2018-12-01 12:00:00',
'2019-03-01 12:00:00',
'2019-06-01 12:00:00',
'2019-09-01 12:00:00',
'2019-12-01 12:00:00',
'2020-03-01 12:00:00',
'2020-06-01 12:00:00',
);

$date_prev = '';
$date_next = '';
$date = '2017-02-10 00:00:00';

$ts_date = strtotime($date); // timestamp of the date we are comparing

foreach( $date_array as $da ){
$ts_da = strtotime($da); // timestamp of the date from array
$ts_prev = strtotime($date_prev); // timestamp of the previous date
$ts_next = strtotime($date_next); // timestamp of the next date
if( $ts_da < $ts_date && ( !$ts_prev || $ts_prev < $ts_da ) )
$date_prev = $da;
if( $ts_da > $ts_date && ( !$ts_next || $ts_next > $ts_da ) )
$date_next = $da;
}

var_dump($date_prev); //string(19) "2016-12-01 12:00:00"
var_dump($date_next); //string(19) "2017-03-01 12:00:00"

Sort array of objects by closest date

What you need to do is work out how far each date is away from today and use this to sort the items, there may be a more elegant way, but this (I think) works. Basically use abs() to ensure all differences are +ve and take each date away from today...

usort($arr, function($a, $b) {
return (abs(strtotime('today') - strtotime($a->date))
- (abs(strtotime('today') - strtotime($b->date))));
});

PHP get closest time to given array of times

As Nico mentioned in the comments, it's pretty straightforward. Just looping and calculating the time difference.

$timeslots = [...];
$expected_time = "2018-12-15T18:00:00.0000000";
$timestamp = strtotime($expected_time);
$diff = null;
$index = null;

foreach ($timeslots as $key => $time) {
$currDiff = abs($timestamp - strtotime($time));
if (is_null($diff) || $currDiff < $diff) {
$index = $key;
$diff = $currDiff;
}
}

echo $timeslots[$index];

Choose from dates array near date

//Start with your array
$array = ["2016-02-22 00:20:00", "2016-02-25 08:45:00", "2016-02-25 19:10:00", "2016-02-25 20:00:00", "2016-02-26 15:55:00", "2016-02-28 17:10:00", "2016-01-22 00:00:00"];

//Set an array called $closestTime that has the time difference and the key
$closestTime = [null,null];

//Check each element in array
foreach($array as $key => $date){

//Calculate difference between now and the time in seconds
$diff = strtotime("now") - strtotime($date);;
if($diff<0) $diff = $diff * -1;

//If $closestTime is empty, populate it
if($closestTime[0]===null) $closestTime = [$diff,$key];

//If $closestTime isn't empty and the current date's time difference
//is smaller, populate $closestTime with the time difference and key
elseif($diff < $closestTime[0]) $closestTime = [$diff,$key];
}

//Print the closest time
echo $array[$closestTime[1]];

//Outputs:
//2016-02-26 15:55:00

Show closest date with name

To calculate nearest key of given date value

$arr =['0' => "2013-02-18 05:14:54",
'1' => "2013-02-12 01:44:03",
'2' => "2013-02-05 16:25:07",
'3' => "2013-01-29 02:00:15",
'4' => "2013-01-27 18:33:45"];
$date = "2013-02-04 14:11:16";
$near = "";
$lowest = 0;
foreach($arr as $key=>$value){
$diff = abs(strtotime($date)-strtotime($value));
if(!$lowest || $diff < $lowest) {
$near = $key;
$lowest = $diff;
}
}
print_r("Nearest key is : $near");

Live demo : https://eval.in/872057

Output will be For

2013-02-04 => 2 // its near to date 2013-02-05

2013-02-06 => 2 // again its near to date 2013-02-05



Related Topics



Leave a reply



Submit