in Italy are 10:00 am if i save a record in my DB with carbon (Carbon::now()) the date is saved as 8.00 am without timezone indication i mean because my laravel app config is set in UTC.
My column is set to $table->dateTimeTz('published_at');
I need to register information about user time zone that i can recover this info for example
2017-01-01T08:00:00+0200
so i can retrive that this news i written at 10 oclock, in user local time and 8.00 UTC time.
Store timezone in database for user (they select in their profile)
If the user inputs a date somewhere, convert to UTC when storing in the db
When displaying dates to the user, convert from UTC to their local timezone
So, everything is stored as UTC in the database and only converted when saving or displaying. I don't use dateTimeTz, just datetime.
// when saving a user supplied date, convert it to UTC
$date = Carbon::parse($userSuppliedDate, auth()->user()->timezone)->setTimezone('UTC');
// When display a date from the database, convert to user timezone.
$date = Carbon::parse($databaseSuppliedDate, 'UTC')->setTimezone($user->timezone);
I create helpers to do the above so I just do {{ localDate($date) }} which converts to user timezone and storeDate($date) when saving. You can also use the $dates array in the model for your custom date fields so you don't have to Carbon::parse() them when retrieving. You'd just convert them like $model->published_at->setTimezone(auth()->user()->timezone)
Thank you @Cronix but if i need to detect timezone of users without he can select it? imagine that my user travel around the world in different location with different time zone, how can i do that ?
@Cronix is correct regarding implementation (I've had to manage software that's international before, it's not easy).
Detecting timezone is not doable on a practical level.
There is a feature in Javascript to get the browser's offset (+2, –5, +6 etc), but even if the offset is right, several timezones might currently have the same offset... but they might change their offset for daylight savings at different times.