Add an option to enable "auto timezone conversion" for MySQL queries. #49412
Replies: 1 comment 4 replies
-
Carbon maintainer here, I wouldn't say
This sounds like you're expecting something to magically guess what timezone you want.
False problem here. Why would you even care to know where is your server and what is its timezone. When developing app, set everything to UTC and assume the application can be deployed anywhere on any server. Same for your database, it could be hosted on a server with a different timezone. So just set your DB timezone to UTC too. BTW you should not even care your DB timezone, as you likely don't want to use any SQL function such as TODAY() or NOW(), it's way better to handle this in your application layer. Timezone is not a thing for the server, it's a client thing, if you're doing stuff for a user, then what you care about is the user context: the timezone on the device the user is currently using (or used last time), if you're doing stuff for a place/event/trip, you care about the timezone there, if you're not in any user or geographical context, you shouldn't work with an arbitrary "server" timezone but with UTC. I go more into details in this article: https://kylekatarnls.medium.com/always-use-utc-dates-and-times-8a8200ca3164 |
Beta Was this translation helpful? Give feedback.
-
Carbon Date Parsing and Timezone Issues
Carbon boasts the capability to parse dates, including those with specified timezones like
"2023-12-17T09:48:30.416754+00:00."
It accurately interprets dates from user-provided inputs along with their specified timezones.Timezone Inheritance in Functions
However, challenges arise when utilizing certain functions, such as
now()
, as they inherit the timezone from theapp.php
configuration. This can lead to unexpected results in date comparisons.Example Scenario
Consider a situation where the server operates in a +2 timezone. If a query compares the
created_at
field to aninput_date
of"2023-12-17T09:48:30.416754+00:00,"
the comparison may yield unexpected results. Even if a record was created at"2023-12-17 11:40:00"
(created_at
), it might satisfy the condition (created_at <= input_date
) because on SQL condition builder, there is no conversion of timezone to the server timezone.I was write solution here: #19737 (comment)
Beta Was this translation helpful? Give feedback.
All reactions