ilya18491's avatar

How should Timezones be handled with multi-timezone customers and APIs

We are facing timezone issues in our Laravel application and database setup.

We are located in +2 timezone and +2 timezone in out config/app => timezone, so all dates in the database are stored in +2 timezone. All models have fields created_at, updated_at and deleted_at that are in 2+ timezone We are working with payment API's and we need to handle datetimes retrieved from them. One of the services returns dates in UTC timezone, another one returns dates in +3 timezone. We faced issue on that step, because dates returned from services includes timezone data and they parsed to correctly by Laravel/Carbon, but when saving to the database, Laravel loses timezone information. So if it was 12:00:00+3, it'll be saved as 12:00:00, but when reading it, Carbon fills it with app timezone (+2), so we have 12:00:00+2

We are using MySQL and Laravel. My questions are: What is the best practice for managing datetime to ensure scalability and avoid issues with customers outside of the +2 timezone? If we use UTC timezone for the database, should we also use UTC as the app timezone? What is the best way to manage customer timezones to ensure accurate reports and payouts? What issues might arise from using either UTC or the +2 timezone?

We considering align all the dates in the database and I have an idea to create custom attribute cast that will call setTimezone before saving to the database and shiftTimezone after loading from the database. My concern here about what time zone should we use, +2 or UTC. If we save everything in UTC, it'll be good when we have customers in other than +2 timezones, but for now I want to be shure that all our logic keep working as expected if we changed timezone. I read some articles and discussions about timezones and mostly it's recomended to use UTC, but we have schedules that based on +2 timezone, Reports generation system, that should work correctly for daily reports, payouts and potentially there are more datetime related things.

0 likes
3 replies
Braunson's avatar

Always set the your app and database timezones to UTC. This way you won't have to deal with any conversion and timezone issues. If you wish to display dates according to your end-user's timezone then convert the date to end-user's timezone just before displaying it (in a view). Avoid storing it in a different timezone.

For example, you save the User's timezone in the db as a setting, when you display dates, you can pull that setting and convert the dates to the user's timezone (from UTC).

There are many other threads asking the same questions here on Laracasts.

As for scheduling reports, I'd suggest payouts in one timezone to keep it simple (your timezone). If you have to have multiple timezones, that makes things harder but you'll have to add more logic.

1 like
Snapey's avatar

As has been said. Deal with everything in UTC.

when scheduling daily reports store the time the report should run in utc but make sure the reporting period is based on date and time not just date, ie, yesterday might be from 2024-04-20 02:00 to 2024-04-21 02:00

see https://youtu.be/jwLzkSyxInM?si=5fdIG4d3gYeAQi0U

1 like
martinbean's avatar

@ilya18491 You should always use and store UTC. Sure, convert dates and times to locale equivalents on your front-end, but always store them as UTC in your database. Timezones get complicated with offsets and daylight savings; UTC does not. It’s constant.

2 likes

Please or to participate in this conversation.