3v4l.org

run code in 300+ PHP versions simultaneously
<?php $dates = array("2012-06-11 08:30:49", "2012-06-07 08:03:54", "2012-05-26 23:04:04", "2012-05-27 08:30:00", "2012-06-08 08:30:55", "2012-06-12 07:45:45"); $children = array("callum" => "1028", "daniel" => "1029"); $closeDate = array(); $firstDate = strtotime("2012-05-29 07:45:45"); function returnClosestDate($firstTime, $dates, $child){ global $initialLoop; global $closeDate; foreach ($dates as $key => $date) { $dateToCompare = strtotime($date); $diff = $firstTime - $dateToCompare; if ($diff < 0) $diff *= -1; if (count($closeDate) == 0) { $closeDate[$key] = array("child" => $child, "date" => $date, "diff" => $diff); continue; } for ($x = 0; $x <= count($closeDate); $x++) { if ($diff < $closeDate[$x]["diff"]) { $closeDate[$x] = array("child" => $child, "date" => $date, "diff" => $diff); echo "<pre>"; print_r($closeDate); echo "</pre>"; exit; } } //if(!alreadyExistInArray($closeDate, $date, $child)){ //if ($diff < $closeDate[$key-1]["diff"]) { //$closeDate[$key] = array("child" => $child, "date" => $date, "diff" => $diff); // } //} } } function alreadyExistInArray($closeDate, $date, $child){ foreach($closeDate as $key => $close){ if($close["child"] === $child) return true; } } foreach($children as $key => $child){ returnClosestDate($firstDate, $dates, $child); }
Output for 5.6.0 - 5.6.38, 7.0.0 - 7.0.33, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
<pre>Array ( [0] => Array ( [child] => 1028 [date] => 2012-06-07 08:03:54 [diff] => 778689 ) ) </pre>
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 <pre>Array ( [0] => Array ( [child] => 1028 [date] => 2012-06-07 08:03:54 [diff] => 778689 ) ) </pre>

preferences:
213.78 ms | 402 KiB | 255 Q