wdelfuego / nova-datetime

Makes working with DateTime fields in Laravel's Nova easier
10 stars 1 forks source link

TimeZone Problem. #6

Open Tanariel opened 1 year ago

Tanariel commented 1 year ago

Related to issue #1

Unfortunately the implemented solution doesn't follow the nova behavior with timezones.

the solution you implemented

public static function withDateFormatFunction() : callable { return function (string $format) { return $this->displayUsing(fn ($d) => ($d instanceof Carbon) ? (config('app.timezone') ? $d->setTimezone(new DateTimeZone(config('app.timezone')))->translatedFormat($format) : $d->translatedFormat($format)) : (($d instanceof DateTimeInterface) ? (config('app.timezone') ? $d->setTimezone(new DateTimeZone(config('app.timezone')))->format($format) : $d->format($format)) : '') ); }; }

is using config('app.timezone') which is the default timezone of the application. But laravel nova is providing a way to personalise the timezone per user : see => https://nova.laravel.com/docs/4.0/resources/date-fields.html#timezones

use Laravel\Nova\Nova;
use Illuminate\Http\Request;

/**

Bootstrap any application services.

@return void
*/
public function boot()
{
parent::boot();

Nova::userTimezone(function (Request $request) {
return $request->user()?->timezone;
});
}

to be compatible with nova and the default customisation of timezone foreach user, the code should somehow look like this instead :

$d->setTimezone(new DateTimeZone($request->user()->timezone ?? config('app.timezone')))->translatedFormat($format)

if you see what i mean ? Actually i'm facing the problem where the app default TZ is UTC, but users are coming from multiple tz and will only see the fields in the app default tz if i use this bundle.

wdelfuego commented 1 year ago

Hey, thanks for reporting and the explanation!

I'll look into this when I can, in the meantime PRs are welcome.