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

SinghWithLaravel's avatar

Change Date timezone in Laravel project while fetching date.

I want to change the datetime while fetching data from database. I could do that through accessors in laravel, but it will just affect the current model. How can i apply it to all tables ?

0 likes
30 replies
skauk's avatar

@singhwithlaravel I belive app.php configuration file has a key timezone which defines the timezone for the application.

SinghWithLaravel's avatar

@skauk, @pom I am working on a project which will used globally, so i can't bind it with a specific timezone. I need to show users time as per their timezone while i am inserting it in UTC. While fetching i am trying to modify time zone as per user's IP using eloqunet mutators. In that case i will have to write date fields mutators function in every Eloquent model. And i don't think that is a good way. What can be done in this case ?

SinghWithLaravel's avatar

@siangboon How can i do that ? Will I need to change it in Every Model for every field ? Can't we something globally for all dates, while saving & fetching ?

kingsleyuchenna's avatar

@skauk This should be the best answer, the article was more than helpful. Clear, straight to the point. Thank you.

SinghWithLaravel's avatar

@skauk @pom @siangboon @jlrdw I saved date as UTC in database, but while fetching i have to show it as per viewer's timezone. I am using Eloquent Accesors. Every date column is going through its accessor method. In every model i have at least 2-3 date accessors. Is it a good practise ? If not, what else can be done ?

skauk's avatar

@singhwithlaravel Please check the article that I have linked in my previous reply. It addresses the very same problem that you are trying to solve. Towards the end, there is a Trait that creates a copy of each date attribute of a model adjusted for the local timezone of the user.

SinghWithLaravel's avatar

@skauk In that case he has overiden toArray function. While i have't used that in my API's. How will it overide ?

skauk's avatar

@singhwithlaravel This is required to add the extra fields with the localized date instead of making accessors for each one like you did.

SinghWithLaravel's avatar

@skauk Let's say i have added all date fields to model. But while fetching i didn't used toArray() method. Now date will not be changed as per user timezone, right ? What can be done at that situation ?

skauk's avatar

@singhwithlaravel The article describes a HasLocalDates Trait that enables you to call $model->localize('date_attribute') and convert any date attribute to user's timezone without having an accessor for each one.

SinghWithLaravel's avatar

@skauk I got that man, but i will have to call $model->localize('date_attribute') this code every time when i will be returning the response. That is the issue i don't want to do things repeatedly.

skauk's avatar

@singhwithlaravel Sorry, I don't quite understand your point. Say you want to display the update timestamp of a model, so you would do $model->updated_at. With the technique described in the article you would do $model->localize('updated_at') instead, so there's nothing extra to do just a different way of accessing the date. Maybe I miss something? Please clarify.

SinghWithLaravel's avatar

@skauk Let's start from starting. I wanted user specific time, so I used timezone field as per your above link. But in the the link all dates(including custom date fields) will be changed only if you are using toArray() in your function while fetching data and i am not using that function, that is why i will have to override some other function. Are you getting me ?

SinghWithLaravel's avatar

@ahkeravi I made two accessors like below code, but while fetching it is not showing what is being passed in accessors for created_at & updated.

   
    /**
     * This will change date according to timezone. 
     * @param String path
     */
    public function getCreatedAtAttribute($value)
    {
        return $this->changeDateFormUTCtoLocal($value);
    }

    /**
     * This will change date according to timezone. 
     * @param String path
     */
    public function getUpdatedAtAttribute($value)
    {
        return $this->changeDateFormUTCtoLocal($value);
    }

But this is not working. While other accessors are working which are following camelCase convention. Personally i assume it as an case issue. I think laravel assumes attributes as camelCase. What can be the solution ?

SinghWithLaravel's avatar

It worked when i did it like below : -

    protected $appends = ['created_at','updated_at'];

    public function getCreatedAtAttribute($value)
    {
        return $this->changeDateFormUTCtoLocal($this->attributes['created_at']);
    }

    /**
     * This will change date according to timezone. 
     * @param String path
     */
    public function getUpdatedAtAttribute($value)
    {
        return $this->changeDateFormUTCtoLocal($this->attributes['updated_at']);
    }
SinghWithLaravel's avatar
Level 2

@ahkeravi The problem was in snakeCase attribute.

public static $snakeAttributes = false;

If it is false you will need to append array of snake cased columns like below.-

    protected $appends = ['created_at','updated_at'];

If you make $snakeAttributes=true, it will work usually.

SinghWithLaravel's avatar

@skauk This was the only solution for my case. Don't you think so ? I had to use accessor in each model to change date.

javeed_rahman's avatar

@singhwithlaravel @skauk can you please share the code i am confused where to put the codes. i tried but not working.

I need to change the date while fetching the records from database

Please or to participate in this conversation.