Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Carbon::now() and now() return the wrong hour in Laravel

Errornow() prints a time several hours behind or ahead of the wall clock

Short answer

Carbon::now() uses the PHP default timezone, and Laravel sets that from app.timezone in config/app.php on every boot. If the hour is off, the config value is not what you think: it is still UTC, an old cached config is in use, or the code runs outside the framework where php.ini decides.

  • Applies to Laravel 12, PHP 8.3, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Compare what Laravel thinks the timezone is with what PHP is using. If the two differ, something runs outside the framework.

    php artisan config:show app
    php -r "echo date_default_timezone_get(), PHP_EOL;"
  2. 2Match what you see, then jump to its fix

    1. 1config:show app lists timezone as UTC.app.timezone is still UTCFix
    2. 2config/app.php shows the right timezone but config:show app or the site still shows the old one.The config cache holds the old valueFix
    3. 3date_default_timezone_get() from a plain PHP script differs from app.timezone.The code runs outside Laravel, so php.ini decidesFix

What does the error look like?

  • now() prints a time several hours behind or ahead of the wall clock
  • created_at values do not match the local time

Cause 1 of 3

app.timezone is still UTC

1Check

Laravel reads the timezone option of config/app.php and passes it to PHP's date_default_timezone_set() while it loads configuration, with UTC as the fallback.

grep -n "timezone" config/app.php

2Fix

Set the timezone you want to your own region in config/app.php, then clear the cache so it is picked up.

// config/app.php
'timezone' => 'Europe/Berlin',

3Verify

Run the first-step command again; the timezone now shows the new value.

php artisan config:show app

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 3

The config cache holds the old value

1Check

After config:cache, Laravel loads the one cached file and does not read your edited config files or .env on later requests.

ls -l bootstrap/cache/config.php

2Fix

Clear the cached configuration, and if you cache on deploy, rebuild it after the edit.

php artisan config:clear
php artisan config:cache

3Verify

Print the value again.

php artisan config:show app

Confirmed by laravel.com

Did this fix it?

Cause 3 of 3

The code runs outside Laravel, so php.ini decides

1Check

PHP picks its default timezone from date_default_timezone_set() if called, then the date.timezone ini setting, and falls back to UTC. A standalone script, a cron job calling php directly or a test bootstrap never runs Laravel's config loading.

php -i | grep -i "date.timezone"
php --ini

2Fix

Set date.timezone in the php.ini that CLI and FPM load, or call date_default_timezone_set() at the start of the script.

; php.ini
date.timezone = Europe/Berlin

3Verify

Run the plain PHP one-liner again.

php -r "echo date_default_timezone_get(), PHP_EOL;"

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Keep app.timezone on UTC for stored data if you serve several regions, and convert only when you show a time to a person.
  • Run config:clear after every change to config/app.php on a server that caches config.
  • Set date.timezone in php.ini on servers so scripts outside Laravel agree with the app.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Why a Filament panel feels slow on a Laravel server

The panel is fast on a small demo and slow once there are many resources, pages and widgets

A slow Filament panel on a server is most often running without the production caches.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.