#UTC Dates and localization

8 messages · Page 1 of 1 (latest)

grave iris
#

OK, so I have a Tasks model and times really aren't important, due_date is casted as 'date:Y-m-d'. The problem though is that when you localize a date on output or even if you dont, if you created a task for Today, its stored as 2023-03-20 00:00:00 and because of that where were in North America, that localized date is now 2023-03-19 17:00:00, etc. Now this is an existing app, so have existing data. So if you create it today, its already past due, etc. Hope I am making sense. How should i go about tackling this? This is the current code they (us, I am new to the project) to set the date fetched to local based on the tenants saved timezone: ```php
public function localize($dateField = null)
{
$dateValue = is_null($this->{$dateField}) ? Carbon::now() : $this->{$dateField};

    return $this->inUsersTimezone($dateValue);
}

/**
 * Change timezone of a carbon date.
 */
private function inUsersTimezone($dateValue): Carbon
{
    return $this->asDateTime($dateValue)->timezone(session_timezone());
}
rocky rampart
grave iris
#

UTC is the default….

ancient fox
#

If you're expecting input from north america, convert dates from whatever timezone the user is in to UTC before storing in the database

drifting salmon
#

I don’t really know what you’re expecting here? As you’ve found, “today” in one location isn’t “today” in another.

#

You should be storing times, even if it is midnight.

#

When a user submits a deadline, you should also know what timezone they’re submitted dates and times in, so you can accurate convert it to the UTC equivalent before storing the database.

#

When querying data, you can then convert it back to whatever timezone.