99 lines
2.7 KiB
PHP
Executable File
99 lines
2.7 KiB
PHP
Executable File
<?php
|
|
|
|
function calculateNextSendTime(string $recurringDays, string $recurringTime): ?string
|
|
{
|
|
$daysMap = [
|
|
'sunday' => 0,
|
|
'monday' => 1,
|
|
'tuesday' => 2,
|
|
'wednesday' => 3,
|
|
'thursday' => 4,
|
|
'friday' => 5,
|
|
'saturday' => 6
|
|
];
|
|
|
|
$days = array_map('trim', explode(',', strtolower($recurringDays)));
|
|
$days = array_filter($days, fn($d) => isset($daysMap[$d]));
|
|
$days = array_map(fn($d) => $daysMap[$d], $days);
|
|
|
|
if (empty($days)) {
|
|
return null;
|
|
}
|
|
|
|
$timeParts = explode(':', $recurringTime);
|
|
$hour = (int) ($timeParts[0] ?? 0);
|
|
$minute = (int) ($timeParts[1] ?? 0);
|
|
|
|
$now = new DateTime('now', new DateTimeZone('America/Mexico_City'));
|
|
$currentDay = (int) $now->format('w');
|
|
$currentHour = (int) $now->format('H');
|
|
$currentMinute = (int) $now->format('i');
|
|
|
|
sort($days);
|
|
|
|
foreach ($days as $day) {
|
|
if ($day > $currentDay || ($day === $currentDay && ($hour > $currentHour || ($hour === $currentHour && $minute > $currentMinute)))) {
|
|
$next = clone $now;
|
|
$next->setTime($hour, $minute);
|
|
$next->modify('+' . ($day - $currentDay) . ' days');
|
|
return $next->format('Y-m-d H:i:s');
|
|
}
|
|
}
|
|
|
|
$daysAhead = (7 - $currentDay) + $days[0];
|
|
$next = clone $now;
|
|
$next->setTime($hour, $minute);
|
|
$next->modify('+' . $daysAhead . ' days');
|
|
|
|
return $next->format('Y-m-d H:i:s');
|
|
}
|
|
|
|
function getRecurringDaysOptions(): array
|
|
{
|
|
return [
|
|
['value' => 'monday', 'label' => 'Lunes'],
|
|
['value' => 'tuesday', 'label' => 'Martes'],
|
|
['value' => 'wednesday', 'label' => 'Miércoles'],
|
|
['value' => 'thursday', 'label' => 'Jueves'],
|
|
['value' => 'friday', 'label' => 'Viernes'],
|
|
['value' => 'saturday', 'label' => 'Sábado'],
|
|
['value' => 'sunday', 'label' => 'Domingo']
|
|
];
|
|
}
|
|
|
|
function formatRecurringDays(string $days): string
|
|
{
|
|
$daysMap = [
|
|
'monday' => 'Lun',
|
|
'tuesday' => 'Mar',
|
|
'wednesday' => 'Mié',
|
|
'thursday' => 'Jue',
|
|
'friday' => 'Vie',
|
|
'saturday' => 'Sáb',
|
|
'sunday' => 'Dom'
|
|
];
|
|
|
|
$daysArray = array_map('trim', explode(',', strtolower($days)));
|
|
|
|
$result = [];
|
|
foreach ($daysArray as $day) {
|
|
$result[] = $daysMap[$day] ?? $day;
|
|
}
|
|
|
|
return implode(', ', $result);
|
|
}
|
|
|
|
function isValidRecurringDays(string $days): bool
|
|
{
|
|
$validDays = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];
|
|
$daysArray = array_map('trim', explode(',', strtolower($days)));
|
|
|
|
foreach ($daysArray as $day) {
|
|
if (!in_array($day, $validDays)) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return !empty($daysArray);
|
|
}
|