From 8fe904eebe00a037e00843a09138d70459bc8f39 Mon Sep 17 00:00:00 2001 From: katamaz Date: Sat, 10 Jan 2026 18:04:41 +0300 Subject: [PATCH] dx v2: stupid ai fix --- backend-php/api.php | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/backend-php/api.php b/backend-php/api.php index a06672a..621a144 100644 --- a/backend-php/api.php +++ b/backend-php/api.php @@ -10,16 +10,29 @@ function db() function findClosestDateSort($array, $targetDate) { - if (!preg_match('/\d{1,2}-\d{1,2}-(\d{2}|\d{4})$/', $targetDate)) return; - $targetTimestamp = DateTime::createFromFormat('d-m-y', $targetDate)->getTimestamp(); - usort($array, function ($a, $b) use ($targetTimestamp) { - $aTimestamp = DateTime::createFromFormat('d-m-y', $a['fromDate'])->getTimestamp(); - $bTimestamp = DateTime::createFromFormat('d-m-y', $b['fromDate'])->getTimestamp(); - $aDiff = abs($aTimestamp - $targetTimestamp); - $bDiff = abs($bTimestamp - $targetTimestamp); - return $aDiff - $bDiff; - }); - return $array[0]; + if (empty($array) || !preg_match('/^\d{1,2}-\d{1,2}-\d{2}$/', $targetDate)) { + return null; + } + + $targetDateTime = DateTime::createFromFormat('d-m-y', $targetDate); + if ($targetDateTime === false) return null; + $targetTimestamp = $targetDateTime->getTimestamp(); + + $result = null; + $maxTimestamp = null; + + foreach ($array as $item) { + $itemDateTime = DateTime::createFromFormat('d-m-y', $item['fromDate']); + if ($itemDateTime === false) continue; + + $itemTimestamp = $itemDateTime->getTimestamp(); + if ($itemTimestamp <= $targetTimestamp && ($maxTimestamp === null || $itemTimestamp > $maxTimestamp)) { + $maxTimestamp = $itemTimestamp; + $result = $item; + } + } + + return $result; } function getDayOfWeek(string $date) {