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

kuns25's avatar

How to Set a Local Timezone for Users

Hii I am using default timezone as America/Los_Angeles , i need to set timezone based on logged in user , I have two tables admins & users both have timezone column, I'm storing created_at timestamps as per los Angeles timezones, in profile setting of the user , one setting is there where user can change timezone to America/Chicago , America/Los_Angeles & America/New_York , so based the user setting i need to change the timezone , following are the methods that i tried before posting question here


    public function getCreatedAtAttribute($date)
    {  
        
     $n = \Carbon\Carbon::parse($date,config('timezone'));

     if (auth()->guard('admin')->user()) {
        $user = auth()->guard('admin')->user(); 
        return $n->setTimezone($user->timezone);
    }

    if (auth()->guard('web')->user()) {
        $user = auth()->user(); 
        return $n->setTimezone($user->timezone);
    }

    return $date;
}

Its working fine but the problem with accessor is that it's not working with SQL join queries

Also tried this by adding below code in appserviceprovider

      if (auth()->guard('admin')->user()) {
        $user = auth()->guard('admin')->user(); 
        config(['app.timezone' => $user->timezone]); 
    }
    elseif (auth()->guard('web')->user()) {
        $user = auth()->user(); 
        config(['app.timezone' =>$user->timezone]); 
    }

above method is not working as data stored in database is as per LA (pacific) timezone i.e 5 am, when changed to Newyork(eastern) timezone , i was expecting 8 am in output

please guide

thanks in advance

0 likes
2 replies
fylzero's avatar

@kuns25 One thing I can tell you is in order to support multiple timezones you should 100% keep your config/app.php timezone setting to UTC.

I would also strongly suggest giving the user the ability to select and change their own timezone setting and persist this to the users table in the database. If you are using Vue, you can pull in moment and moment-timezone to build out a cool selector component on sign up that guesses the users current timezone like so...

<template>
    <select name="timezone" id="timezone" class="form-control" required>
        <option
            v-for="timezone in JSON.parse(timezones)"
            :key="timezone"
            :value="timezone"
            :selected="timezone == tzGuess"
        >
            {{ timezone }}
        </option>
    </select>
</template>

<script>
    import moment from 'moment';
    require('moment-timezone');
    export default {
        props: ['timezones'],
        computed: {
            tzGuess() {
                return moment.tz.guess(true);
            }
        }
    }
</script>
<time-zone-select timezones="{{ json_encode(timezone_identifiers_list(), JSON_UNESCAPED_SLASHES) }}"></time-zone-select>

Then you essentially wind up just having to convert timezones on the way into and out of the database to and from UTC. The power of this is that UTC gives you a baseline to always convert from.

You can pass the timezone to moment like so...

var timezone = 'US/Central'; // Pass this through props or get from the database directly
moment.tz(timezone).format('dddd, MMMM Do YYYY, h:mm:ss a')

Then you can use Carbon to parse timezones in PHP as well... the user timezone in parse tells it to parse the passed time using the user's timezone, timezone('UTC') then converts that to UTC to use for a date range search or something programatically.

Carbon::parse($request->report_start, auth()->user()->timezone)
            ->startOfDay()
            ->timezone('UTC')
            ->toDateTimeString()

When storing time in the database, typically most date pickers are timezone aware and pass the converted UTC time, so you usually don't need to do anything. However, this is not guaranteed and you'll want to check everything when you implement new date pickers and such.

Final note. What you are trying to do above by switching the config timezone entirely is going to cause you massive headaches. I'd strongly advise against using that approach.

3 likes
chiefguru's avatar

We store all of our date times in UTC and every student/staffer has a location field. We use a simple configuration to find which timezone belongs to that location and change all of our display and calculation datetimes to that timezone for that student/staffer.

Admittedly we only deal with 6 locations and 4 timezones, so a config file is fast and easy.

1 like

Please or to participate in this conversation.