Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

asdrubal's avatar

Carbon - store from local timezone to UTC

Hi

I have a "time" field in Db which is storing just time in format HH:MM:SS. User table contains field "timezone" which stores string like 'Asia/Kolkata' and few others. In model I have:

public function getTimeAttribute ($time){
return Carbon::parse($time)->setTimezone(Auth::user()->timezone)->format('H:i');}

which displayes what I need, so time stored in UTC + time difference resulting from timezone field. What I do not know how to do is the other way round, so how to store this time from input in local timezone to UTC. So on the form I specify 18:00 and it stores it as 18:00:00 not taking timezone into account.

Any help please.

0 likes
1 reply
tykus's avatar
tykus
Best Answer
Level 104

You can define a mutator which to convert the time to UTC - the second argument to parse sets the original timezone:

public function setTimeAttribute($value)
{
        $this->attributes['time'] = Carbon::parse($value, Auth::user()->timezone)
                ->setTimezone('UTC)->format('H:i');
}
5 likes

Please or to participate in this conversation.