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

raziel79's avatar

Date UTC + Timezone in DB column

Hi all,

i need explanation about date in Laravel.

My server is located in Italy so for example:

in Italy are 10:00 am if i save a record in my DB with carbon (Carbon::now()) the date is saved as 8.00 am without timezone indication i mean because my laravel app config is set in UTC.

My column is set to $table->dateTimeTz('published_at');

I need to register information about user time zone that i can recover this info for example

2017-01-01T08:00:00+0200

so i can retrive that this news i written at 10 oclock, in user local time and 8.00 UTC time.

can anyone explain me how can i do that?

Thank you all.

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

Here's what I do:

  1. Store timezone in database for user (they select in their profile)
  2. If the user inputs a date somewhere, convert to UTC when storing in the db
  3. When displaying dates to the user, convert from UTC to their local timezone

So, everything is stored as UTC in the database and only converted when saving or displaying. I don't use dateTimeTz, just datetime.

// when saving a user supplied date, convert it to UTC
$date = Carbon::parse($userSuppliedDate, auth()->user()->timezone)->setTimezone('UTC');

// When display a date from the database, convert to user timezone.
$date = Carbon::parse($databaseSuppliedDate, 'UTC')->setTimezone($user->timezone);

I create helpers to do the above so I just do {{ localDate($date) }} which converts to user timezone and storeDate($date) when saving. You can also use the $dates array in the model for your custom date fields so you don't have to Carbon::parse() them when retrieving. You'd just convert them like $model->published_at->setTimezone(auth()->user()->timezone)

7 likes
raziel79's avatar

Thank you @Cronix but if i need to detect timezone of users without he can select it? imagine that my user travel around the world in different location with different time zone, how can i do that ?

gregrobson's avatar

@Cronix is correct regarding implementation (I've had to manage software that's international before, it's not easy).

Detecting timezone is not doable on a practical level. There is a feature in Javascript to get the browser's offset (+2, –5, +6 etc), but even if the offset is right, several timezones might currently have the same offset... but they might change their offset for daylight savings at different times.

If most of your users are Italian then I would default the setting to 'Europe/Rome'. http://php.net/manual/en/timezones.europe.php

1 like

Please or to participate in this conversation.