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

Snapey's avatar
Level 122

date format per timezone

I'm wondering if anyone knows of a service/package that might convert a user entered timezone into a likely format for date/times to be presented back to them.

My client's site asks the user for their timezone, and they record all times in the DB in UTC then apply the timezone when displaying date/times but we don't know if people in that timezone like to see dates as dd/mm/yy or mm/dd/yy or dd.mm.yyyy etc

0 likes
8 replies
Snapey's avatar
Level 122

@jlrdw Thanks for the input, but neither of these address the problem, which is one of display

The Carbon issue is related to just setting the timezone and then outputs the format in a specific format, not user timezone dependent formatting.

eg

echo now()
    ->tz('America/Chicago')
    ->toDayDateTimeString();

echo now()
    ->tz('Europe/London')
    ->toDayDateTimeString();

echo now()
    ->tz('Europe/Berlin')
    ->toDayDateTimeString();

echo now()
    ->tz('Asia/Tokyo')
    ->toDayDateTimeString();

outputs

Fri, Mar 19, 2021 2:55 PM
Fri, Mar 19, 2021 7:55 PM
Fri, Mar 19, 2021 8:55 PM
Sat, Mar 20, 2021 4:55 AM

So, it works in that it writes the date out in an unambiguous format, but its not as per my requirements.

jlrdw's avatar

@snapey I thank the second medium article in that issue goes into more detail. But it won't hurt to check.

Edit, I had only looked at the second one the first one seems to be more of a PHP solution.

MichalOravec's avatar
echo now()->tz('Europe/Berlin')->locale('de')->isoFormat('LLLL');

echo now()->tz('America/Chicago')->locale('en_US')->isoFormat('LLLL');

echo now()->tz('Europe/London')->locale('en_GB')->isoFormat('LLLL');

Outputs

Freitag, 19. März 2021 21:14

Friday, March 19, 2021 3:14 PM

Friday, 19 March 2021 20:14

And instead of LLLL you can use L, LL, LLL, LT, LTS as well.

https://github.com/briannesbitt/Carbon/blob/master/src/Carbon/Lang/en_GB.php

https://github.com/briannesbitt/Carbon/blob/master/src/Carbon/Lang/de.php#L81

Here you find other languages

https://github.com/briannesbitt/Carbon/tree/master/src/Carbon/Lang

Docs: https://carbon.nesbot.com/docs/#api-localization (look for Some macro-formats are also available. Here are examples of each in some languages)

jlrdw's avatar

@snapey any way you can have them enter locale as well?

Edit: You could have a dropdown where they pick what format they want to see, and relate that however you need, storing choice in user table or related attributes table.

choice 3 = a certain format

Probably have a config array for it. That's what I'd probably do.

Please or to participate in this conversation.