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

clemask's avatar

Models carbon object setting time zone and date format has no effect #48809

Laravel Version 10.24.0

$carbon = new Carbon();
$carbon->settings([
            'toStringFormat' => 'Y/m/d/H:i'
]);
echo $carbon . PHP_EOL;

$record = (new CustomModel())->find(33);
$record->created_at->setTimezone(new \DateTimeZone(timezone_to_gmt(8)))->settings([
            'toStringFormat' => 'Y/m/d/H:i'
 ]);

echo $record->created_at . PHP_EOL;

output

2023/10/25/01:43
2023-10-25 01:43:18
0 likes
5 replies
jlrdw's avatar

Not positive of what you are doing, seems you want to display a users timezone.

Of course store in database UTC. But each user should have their timezone stored so you can easily use their timezone for display.

Here is an older post, have a look. There are some other good past post as well.

https://laracasts.com/discuss/channels/general-discussion/eloquent-carbon-and-displaying-dates-in-a-users-local-timezone

ANd https://laracasts.com/discuss/channels/laravel/displaying-dates-accordingly-to-each-timezone

clemask's avatar

@jlrdw Thanks! The user's timezone can be switched. I am storing times in UTC 0 timezone. I need to convert the times based on the user's timezone to display the time in the user's selected timezone. However, directly converting the model's datetime fields is not working.

jlrdw's avatar

@clemask practice with this code:

        $n = now();
        $date = new \DateTime($n);
        $date->setTimezone(new \DateTimeZone('UTC'));
        echo $date->format('Y-m-d H:i:s T');
        $date->setTimezone(new \DateTimeZone('America/Chicago'));
        echo '<br>';
        echo $date->format('Y-m-d H:i:s T');
        die;

Have UTC stored, get the users timezone from (probably a profile table). Convert and display the users timezone.

Above just example, use variables of course.

Run that code and see result in browser.

Edit:

I did not use carbon, convert as needed. If you get get it worked out with carbon, please post result, I'd like to make a gist on this showing php and carbon since this question seems to come up often.

clemask's avatar

@jlrdw If I instantiate a Carbon object to handle dates, it works fine. However, in Laravel 10, when the model's date attribute is a Carbon object, it does not work properly. I suspect this may be a Laravel issue.

I opened a bug report on GitHub in the laravel/framework repository, but it was closed. They suggested I come here to find an answer.

jlrdw's avatar

@clemask

when the model's date attribute is a Carbon object

I don't even know what that means. Mysql has either a timestamp or a datetime depending on the field.

Forget terms like model object, rather it is a database record with a field type of one of the two I mentioned. I still wonder where some of these terms come from.

The straight php functions I gave you will work.

Please or to participate in this conversation.