Files
onboard/app/Services/DateRange.php
2026-03-25 17:37:32 +09:00

100 lines
2.2 KiB
PHP

<?php
namespace App\Services;
use Carbon\Carbon;
use Illuminate\Support\Collection;
readonly class DateRange
{
public function __construct(
public Carbon $startDate,
public Carbon $endDate,
public string $startDateRaw,
public string $endDateRaw,
public bool $isOneDay
) {}
/**
* Получить начало диапазона в Carbon
*/
public function start(): Carbon
{
return $this->startDate;
}
/**
* Получить конец диапазона в Carbon
*/
public function end(): Carbon
{
return $this->endDate;
}
/**
* Получить начало диапазона в SQL формате
*/
public function startSql(): string
{
return $this->startDate->format('Y-m-d H:i:s');
}
/**
* Получить конец диапазона в SQL формате
*/
public function endSql(): string
{
return $this->endDate->format('Y-m-d H:i:s');
}
/**
* Получить начало диапазона как timestamp
*/
public function startTimestamp(): int
{
return $this->startDate->getTimestampMs();
}
/**
* Получить конец диапазона как timestamp
*/
public function endTimestamp(): int
{
return $this->endDate->getTimestampMs();
}
public function startFirstOfMonth()
{
return $this->startDate->firstOfMonth()->setHour(6)->format('Y-m-d H:i:s');
}
public function endFirstOfMonth()
{
return $this->endDate->firstOfMonth()->setHour(6)->format('Y-m-d H:i:s');
}
/**
* Проверить, является ли дата сегодняшней
*/
public function isEndDateToday(): bool
{
return $this->endDate->isToday();
}
/**
* Получить диапазон дней
*/
public function getDaysRange(): Collection
{
$days = collect();
$current = $this->startDate->copy();
while ($current->lte($this->endDate)) {
$days->push($current->copy());
$current->addDay();
}
return $days;
}
}