Comments & Discussion
Question How can I add the timezone to the Profile so that if the client moves thay can change the timezone for them? Also with in a user edit page?
Hi, I always store timestamp data in UTC timezone. How to solve Daylight Saving Time issue when convert to local timezome that don't have DST.
Hi, could you give more details on this?
It should in theory convert the timestamps automatically if:
- Your application is set to UTC
- Your database is set to UTC
- You convert the date from user date to UTC on saving moment
- You convertthe date from UTC to user on display
If that doesn't happen - I would love to get some examples to check. DST is built into PHP and should cover your case completely.
I always store created_at timestamp field data in UTC timezone and my application also based on UTC.
So, I just put a function in my Model like this to display datetime based on my local timezone.
protected function serializeDate(DateTimeInterface $date): string { return $date->timezone('Asia/Jakarta')->format('d/m/Y H:i:s); }
However, I encountered an issue regarding Daylight Saving Time.
Normally, when displaying datetime to 'Asia/Jakarta' timezone, the system should add +7 hours, and during Daylight Saving Time +8 hours.
But, at the moment, the system always keeps adding +7 hours.
Could you please advise, how the best way to solve this issue?
Hi, I am not sure why we need the middleware "SetTimezoneMiddleware"? What happen if we don't use this middleware in all routes?
Beside, in store method of BookingController, 'start' => fromUserDateTime($request->validated('start')),, as I understand, the function fromUserDateTime above will alway convert $request->validated('start') (datetime local of client) in UTC timezone to UTC timezone, so it seems redundant. I think it only makes sense if we also pass auth()->user() as second parameter as follow 'start' => fromUserDateTime($request->validated('start'), auth()->user()),, so it will convert start in user's timezone to UTC timezone before saving in database.
You need that middleware as it then globally informs your Carbon package that it should use the specific timezone. If you do not do that - it will simply always use the one from configuration.
As for your question about fromUserDateTime - This function also uses that global Carbon setting. You don't have to pass the timezone/user to it, as that is taken from the Middleware configuration.
I see, but in this logic if (is_string($date)) { return Carbon::parse($date, $timezone)->setTimezone('UTC')->toDateTimeString(); }, when $date is string, the function will convert $date from $timezone(which is default to UTC in this case, not the timezone of user which was set in SetTimezoneMiddleware) to UTC
In my case, I needed to make some modifications to make it work properly because I encountered some problems.
- I modified the Middleware to apply the date/time conversion only in the GET method, thus preventing the timestamps (created_at, updated_at) from being updated with the user's configuration and not the system's. Thus, when a date/time field is filled in, it only needs to use the
fromUserDateTimehelper (for example).
if($request->method() === 'GET') {
// Code here
}
- Still in Middleware, if there is also another middleware that also modifies the Language using
App::setLocale, it is better to useCarbon::setLocale(app()->getLocale());thanCarbon::setLocale($localeCode);coming fromDateTimeZone.
In my case, using DateTimeZone, it returned only "pt" and not "pt-BR" or "pt_BR", and when using the diffForHumans() method the text was not in the correct language.
- Because of the
date_default_timezone_setfunction in the Middleware, I also needed to modify thetoUser* helpers, to useCarbon::parsewhen the$dateis of the Carbon type because it was not working correctly.
toUserDateTime(string|Carbon $date, ?User $user = null, string $timezone = 'UTC')
{
// .....
if (is_string($date)) {
return Carbon::parse($date, 'UTC')->setTimezone($timezone)->format($user->datetime_format);
}
return Carbon::parse($date->format('Y-m-d H:i:s'), 'UTC')->setTimezone($timezone)->format($user->datetime_format);
}
Povilas Korop I am currently in the proses of combining you Travel course and this one in the same project so any help you can give along this lines will be appreciated.
Thanks
Sorry, not sure what help you need here.