dx v2: stupid ai fix

This commit is contained in:
2026-01-10 18:04:41 +03:00
parent ff1174671a
commit 8fe904eebe
+23 -10
View File
@@ -10,16 +10,29 @@ function db()
function findClosestDateSort($array, $targetDate) function findClosestDateSort($array, $targetDate)
{ {
if (!preg_match('/\d{1,2}-\d{1,2}-(\d{2}|\d{4})$/', $targetDate)) return; if (empty($array) || !preg_match('/^\d{1,2}-\d{1,2}-\d{2}$/', $targetDate)) {
$targetTimestamp = DateTime::createFromFormat('d-m-y', $targetDate)->getTimestamp(); return null;
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(); $targetDateTime = DateTime::createFromFormat('d-m-y', $targetDate);
$aDiff = abs($aTimestamp - $targetTimestamp); if ($targetDateTime === false) return null;
$bDiff = abs($bTimestamp - $targetTimestamp); $targetTimestamp = $targetDateTime->getTimestamp();
return $aDiff - $bDiff;
}); $result = null;
return $array[0]; $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) function getDayOfWeek(string $date)
{ {