Laravel Nova: DateTime in Model Timezone
Asked Answered
M

1

6

I want Laravel Nova to handle a DateTime field (stored in the database in UTC) in a custom timezone based on the Model and not in the User's timezone it does by default:

https://nova.laravel.com/docs/3.0/resources/date-fields.html#timezones

The field is for event start times at various locations across timezones throughout the United States. It does not make sense to display them in the user's timezone since they will be sorted with a location filter or viewed in respect to the Location model.

Does anyone have a solution for how to handle this use case?

Macrae answered 7/10, 2020 at 20:40 Comment(1)
For the index, I'm fine with doing it as a text field with a function: https://mcmap.net/q/1916157/-laravel-nova-diffforhumans-datetimeMacrae
B
2

Try this instead, create new column in users table called timezone.

Schema::table('users', function (Blueprint $table) {
        $table->string('timezone')->nullable();
    });

In User resource create timezone field like this

Timezone::make('Timezone')
                ->default('yourtimezone'),

then in novaServiceProvider add following in boot()

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

this allows you to have control over user timezones. Once user is logged in with the specific timezone he/she will be able to create events at various locations across timezones.

Bronze answered 7/10, 2021 at 10:21 Comment(2)
Thank you but want model-specific time zone, not user-specific time zone.Sohn
You can achieve it using above method. Its not user specific its user model specific you can change user model to location model. Create col in location model. Each record of location will have timestamp and when user logs in you can check for its location and retrieve timestamp of that location.Bronze

© 2022 - 2024 — McMap. All rights reserved.