Support the ongoing development of Laravel.io →
posted 11 years ago

I've been running into a problem with getting dates to display correctly, and I think I've been overusing the timezone() feature of Carbon. Am I correct in thinking that timezone() is unnecessary when storing and displaying date information which is stored in a MySQL field of type DATE? I've been getting birthdates stored correctly in MySQL after applying timezone('America/Detroit') before storage, but when I applied it before displaying them post-retrieval from the database, they were showing one day earlier than the value stored.

I'm figuring that applying timezone() to the Carbon object prior to storage in MySQL was adding 5 hours to get to UTC (bumping it from 12:00:00 am to 5 am), the date was then stored with no time component, and then on retrieval it was defaulting to 12am, and applying timezone subtracted 5 hours, cycling the calendar back one day. Does that sound right?

Last updated 3 years ago.
0

MySQL DATE/DATETIME fields do not store any timezone information, so it's usually regarded best to set your application's timezone to UTC, store datetime information as UTC and then adjust timezones on retrieval.

Storing DATE data sounds like just an awful idea if you're working with timezones. There's no information about whether it's 2am or noon, so there's no way to know if subtracting 5 hours will change the date or not.

Last updated 3 years ago.
0

I have a blog post here about how to manage this:

http://andrew.cool/blog/49/Easy-timezones-in-Laravel-with-Carbon

Basically override the Eloquent asDateTime function to apply a timezone offset. That way everything is stored in UTC but displayed in the current user's timezone. There is one caveat which is that when setting any timestamp you must set it to a Carbon instance and not a string.

Dates (vs. Datetimes) should not have a timezone at all. People celebrate Christmas on December 25th. It's not "December 25th at 12:00 am UTC." People in Chicago don't celebrate Christmas on December 24th at 7 pm. It's on that date no matter what timezone you're in. If you do have something that is taking place "at 12:00 am" in a specific timezone, then that's not a date, it's a Datetime, and you're just confused that it's a date because it takes place at midnight.

Last updated 9 years ago.
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

dspitzle dspitzle Joined 31 Jan 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.