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

Stratos's avatar
Level 14

Displaying dates accordingly to each timezone.

So let's say I create and store in a database a cupon for a discount that is available starting from tomorrow 4pm LOCALE TIME (GMT-7), this data is stored in a "starts_at" field in UTC format so it would be 11 pm in UTC format.

So if it's next day 1pm in my locale (GMT-7) I want it to say "this coupon will be available in 3 hours" instead of 10 hours (which would be 3 hours + 7 difference from utc)

How to achieve this?

Need it this way because people might see it from different timezones, and I want it to tell them how long for it to start (or how long time left, etc) according to their timezone.

0 likes
5 replies
Cronix's avatar
Cronix
Best Answer
Level 67

You'll need to store the users timezone in their profile or users table. Then when outputting/displaying dates, just format it using the users timezone. created_at and updated_at are automatically converted to carbon instances, which makes this easier. If you have other dates that you're using, add them to the protected $dates array on the model and laravel will convert them to carbon instance too and then you can use carbons setTimezone() to change the date/time to the users timezone.

Then you'd just:

$userTimezone = 'America/Vancouver';
echo $your_db_result->created_at->setTimezone($userTimezone);

If you are accepting date/times from user input, that will be in their local timezone. When storing those dates, you'll need to convert back to UTC before storing in the db using $user_supplied_date->setTimezone('UTC').

2 likes
rahul_singh_codoffer's avatar

How can we format the dates form the time zone. Like if user belongs to the uk i want to show the dates in dd-mm-yyyy format but if user is belongs to USA then i want to show the dates in the mm-dd-yyyy format. how can i do this using the time zone , i don't want to store the date format for every user. can i achieve this using the time zone ? because user can belongs from any timezone world wide.

Please or to participate in this conversation.