From adde7c8d9852bf4bb349088c16a2b29a4d86a53e Mon Sep 17 00:00:00 2001 From: Jakob Osterberger <98315600+jk-oster@users.noreply.github.com> Date: Thu, 3 Oct 2024 11:56:02 +0000 Subject: [PATCH] Use isoFormat() instead of format() for placeholder dates Fixing the issue that Cardon format() does not format the date for weeks "Y-W" correctly with dates that belong to the last week of the previous year. E.g. "2022-01-02" is actually "2021-52", however usual format method would incorrectly output "2022-52" --- src/Trend.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Trend.php b/src/Trend.php index 28ff762..032d354 100755 --- a/src/Trend.php +++ b/src/Trend.php @@ -146,7 +146,7 @@ public function mapValuesToDates(Collection $values): Collection $placeholders = $this->getDatePeriod()->map( fn (CarbonInterface $date) => new TrendValue( - date: $date->format($this->getCarbonDateFormat()), + date: $date->isoFormat($this->getCarbonDateFormat()), aggregate: 0, ) ); @@ -183,13 +183,13 @@ protected function getSqlDate(): string protected function getCarbonDateFormat(): string { return match ($this->interval) { - 'minute' => 'Y-m-d H:i:00', - 'hour' => 'Y-m-d H:00', - 'day' => 'Y-m-d', - 'week' => 'Y-W', - 'month' => 'Y-m', - 'year' => 'Y', - default => throw new Error('Invalid interval.'), + 'minute' => 'YYYY-MM-DD HH:mm:00', + 'hour' => 'YYYY-MM-DD HH:00', + 'day' => 'YYYY-MM-DD', + 'week' => 'GGGG-WW', + 'month' => 'YYYY-MM', + 'year' => 'YYYY', + default => throw new Error('Invalid interval. Possible intervals: "minute", "hour", "day", "week", "month", "year".') }; } }