fix: improve date calculation for month-end reset schedules
This commit is contained in:
@@ -130,17 +130,23 @@ class TrafficResetService
|
|||||||
$expiredAt = Carbon::createFromTimestamp($user->expired_at, config('app.timezone'));
|
$expiredAt = Carbon::createFromTimestamp($user->expired_at, config('app.timezone'));
|
||||||
$resetDay = $expiredAt->day;
|
$resetDay = $expiredAt->day;
|
||||||
$resetTime = [$expiredAt->hour, $expiredAt->minute, $expiredAt->second];
|
$resetTime = [$expiredAt->hour, $expiredAt->minute, $expiredAt->second];
|
||||||
// 当前月目标时间
|
|
||||||
$currentMonthTarget = $from->copy()->day($resetDay)
|
$currentMonthTarget = $from->copy()->day($resetDay)->setTime(...$resetTime);
|
||||||
->setTime(...$resetTime);
|
|
||||||
if ($currentMonthTarget->timestamp > $from->timestamp) {
|
if ($currentMonthTarget->timestamp > $from->timestamp) {
|
||||||
return $currentMonthTarget;
|
return $currentMonthTarget;
|
||||||
}
|
}
|
||||||
// 下月目标时间
|
|
||||||
$nextMonth = $from->copy()->addMonth();
|
$nextMonthTarget = $from->copy()->startOfMonth()->addMonths(1)->day($resetDay)->setTime(...$resetTime);
|
||||||
$lastDayOfNextMonth = $nextMonth->copy()->endOfMonth()->day;
|
|
||||||
$targetDay = min($resetDay, $lastDayOfNextMonth);
|
if ($nextMonthTarget->month !== ($from->month % 12) + 1) {
|
||||||
return $nextMonth->copy()->day($targetDay)->setTime(...$resetTime);
|
$nextMonth = ($from->month % 12) + 1;
|
||||||
|
$nextYear = $from->year + ($from->month === 12 ? 1 : 0);
|
||||||
|
$lastDayOfNextMonth = Carbon::create($nextYear, $nextMonth, 1)->endOfMonth()->day;
|
||||||
|
$targetDay = min($resetDay, $lastDayOfNextMonth);
|
||||||
|
$nextMonthTarget = Carbon::create($nextYear, $nextMonth, $targetDay)->setTime(...$resetTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $nextMonthTarget;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -171,10 +177,17 @@ class TrafficResetService
|
|||||||
if ($currentYearTarget->timestamp > $from->timestamp) {
|
if ($currentYearTarget->timestamp > $from->timestamp) {
|
||||||
return $currentYearTarget;
|
return $currentYearTarget;
|
||||||
}
|
}
|
||||||
$nextYear = $from->copy()->addYear();
|
|
||||||
$lastDayOfMonth = $nextYear->copy()->month($resetMonth)->endOfMonth()->day;
|
$nextYearTarget = $from->copy()->startOfYear()->addYears(1)->month($resetMonth)->day($resetDay)->setTime(...$resetTime);
|
||||||
$targetDay = min($resetDay, $lastDayOfMonth);
|
|
||||||
return $nextYear->copy()->month($resetMonth)->day($targetDay)->setTime(...$resetTime);
|
if ($nextYearTarget->month !== $resetMonth) {
|
||||||
|
$nextYear = $from->year + 1;
|
||||||
|
$lastDayOfMonth = Carbon::create($nextYear, $resetMonth, 1)->endOfMonth()->day;
|
||||||
|
$targetDay = min($resetDay, $lastDayOfMonth);
|
||||||
|
$nextYearTarget = Carbon::create($nextYear, $resetMonth, $targetDay)->setTime(...$resetTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $nextYearTarget;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user