laravel-trend icon indicating copy to clipboard operation
laravel-trend copied to clipboard

[Question / Feature ] Manage timezone

Open aeq-dev opened this issue 1 year ago • 1 comments

Hi, Thanks for this nice package, Please how could we manage queries timezone if we use per day (hours) option ?

aeq-dev avatar Dec 26 '23 12:12 aeq-dev

I have the same question - when displaying hourly data over a day, it is off by 2 hours because of different timezone capture.

mberneis avatar Apr 07 '24 23:04 mberneis

Hi,

I guess you have the original data in UTC timezone and have to display it with the user/app timezone. The data is correctly aggregated you just have to manipulate the labels.

This is an example to displey the last 72 hours...

You can manipulate the labels like this:

        $dataA = Trend::model(ActaAyuntamiento::class)
            ->between(
                start: now()->addHours(-72),
                end: now(),
            )
            ->perHour()
            ->count('*');

        return [
            'datasets' => [
                [
                    'label' => 'Actas Ayuntamiento',
                    'data' => $dataA->map(fn (TrendValue $value) => $value->aggregate),
                    'backgroundColor' => '#797979',
                    'borderColor' => '#797979',
                    'stack' => 'actas',
                ],
            ],
            'labels' => $dataA->map(fn (TrendValue $value) => Carbon::createFromFormat('Y-m-d H:i', $value->date, 'UTC')
                ->timezone('America/Monterrey')
                ->format('H:i')),
            ];

Hope it helps.

ramnzys avatar Jun 04 '24 00:06 ramnzys

Thank you so much @ramnzys :)))

aeq-dev avatar Jun 13 '24 11:06 aeq-dev