DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Pradeep Kumar
Pradeep Kumar

Posted on

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.

Top comments (0)

🌚 Life is too short to browse without dark mode