<?php $today = '2019-03-20 14:27:06'; $dates = [ '2019-03-20 09:03:00', '2019-03-20 15:12:29', '2019-03-20 09:39:11', '2019-03-20 12:58:22', '2019-03-20 19:14:47', '2019-03-21 09:08:38' ]; function getNearestDate(array $dates, ?string $today = null) { if (!$today) { $today = date('Y-m-d H:i:s'); } $fnDT = function($d) { return DateTime::createFromFormat('Y-m-d H:i:s', $d); }; usort($dates, function($a, $b) use ($fnDT, $today) { $da = abs($fnDT($today)->getTimestamp() - $fnDT($a)->getTimestamp()); $db = abs($fnDT($today)->getTimestamp() - $fnDT($b)->getTimestamp()); return $da - $db; }); $nearest = $dates[0]; if ($fnDT($nearest)->format('Y-m-d') !== $fnDT($today)->format('Y-m-d')) { print_r('No date of the same day.'); return false; } return $nearest; } print_r(getNearestDate($dates, $today));
You have javascript disabled. You will not be able to edit any code.