Fixed: WHM (CPanel) + Laravel Timezone Issue

themodernpk

Pradeep Kumar

Posted on August 5, 2022

Fixed: WHM (CPanel) + Laravel Timezone Issue

While hosting Laravel on WHM control panel, you need to make sure to set correct timezone at 4 places:

1. MySql Timezone
MySql time zone is taken from server time which can be update from:

WHM Login > Server Configuration > Server Time
Enter fullscreen mode Exit fullscreen mode

After update the Timezone you need to restart the server.

2. PHP Timezone
This will come from php.ini file. To update php.ini file you need to visit:

WHM Login > MultiPHP Manager > Editor Mode > Choose the PHP Version
Enter fullscreen mode Exit fullscreen mode

Update following variable:

date.timezone = "UTC"
Enter fullscreen mode Exit fullscreen mode

Restart apache and php-fpm service.

3. Laravel Timezone
Update laravel timezone in config/app.php:

...
'timezone' => 'UTC',
...

Enter fullscreen mode Exit fullscreen mode

4. Serialize Datetime
In your Laravel models, use following code:

protected function serializeDate(\DateTimeInterface $date)
{
    return $date->format('Y-m-d H:i:s');
}
Enter fullscreen mode Exit fullscreen mode

You can also create a Base.php model with the above code and extend all your models to Base model.

💖 💪 🙅 🚩
themodernpk
Pradeep Kumar

Posted on August 5, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related