DatePicker creating records wrongly but editing correctly

I have an app configured with UTC timezone, and form with the following date picker (DATE ONLY, no time):
Forms\Components\DatePicker::make('start_date')
->label('Data de início')
->timezone('America/Sao_Paulo')
->required(),
Forms\Components\DatePicker::make('start_date')
->label('Data de início')
->timezone('America/Sao_Paulo')
->required(),
If i select 2023-03-09 in the form, it CREATES in the database as 2023-03-09 (wrong, it should save as D-1) If i view the record, it will show as 2023-03-08 (wrong, because of timezone) Then i edit this date back to 2023-03-09 and it UPDATES to 2023-03-10 (correct, D-1) If i view the record, it will show as 2023-03-09 (which is now correct) WTF is going on? Am i dumb and don't know how timezone works?
13 Replies
Saade
SaadeOP2y ago
Ok, looking through some issues, i found timezone() method is not appropriate for use on a date-only input - only time. which makes sense (duh). But the date still jumps back 1 day while creating, but not when saving, what is going on in this case?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
awcodes
awcodes2y ago
Is it cast as a datetime?
Saade
SaadeOP2y ago
date
awcodes
awcodes2y ago
That’s the only thing I could think of. Sorry.
Saade
SaadeOP2y ago
btw, i have separated columns for start_date start_time end_date end_time i dont really know to to compute timezones when using separated fields with start_time and end_time being nullable
awcodes
awcodes2y ago
Yea. That’s tricky. The date is whatever they enter. Do they need to be separate in the datebase? If the time is null it’ll just default to 00:00:00
Saade
SaadeOP2y ago
but then how do i deferentiate 2023-03-10 00:00:00 "today with null time" from "today at midnight"? its an "event in the calendar" which an event can have an start time or not
awcodes
awcodes2y ago
I’m not sure. Just trying to think of options.
Saade
SaadeOP2y ago
its ok 🙂 Maybe Zep can help in some way, lets see if he shows up in here
Thijmen
Thijmen2y ago
Why do you need the timezone? Because timezones only apply to times and not dates i am not sure i understand your use case. The difference between utc and sao paulo is like 5 hours. So the only time when UTC and Brasil time are different is in the last 5 hours of Brasil time. So if a brasilian user selects the date 2023-03-10 on 10:24 that will also be 2023-03-10 on UTC time. I think the create is not timezone based. Only the select is rendering the different date and then it is changed because the select states a new date
wyChoong
wyChoong2y ago
In my case of just using without time, I store start date and end date. And when using it, then set the time to 00:00 and 23:59 respectively , then I can get the range of start date time to end date time for same day or more than a day event
Saade
SaadeOP2y ago
Hi, thanks for the response America/Sao_Paulo has a 3 hour difference (UTC-3), therefore if i select 2023-03-10 on 10:24pm in America/Sao_Paulo it will be 2023-03-11 on 01:24 in UTC in that example i need to add +1 day to the date

Did you find this page helpful?