Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mknylund's avatar

Format from datetime-local input to dateTime in database

I have an input with type datetime-local. In my migration I have set up dateTime, but when I fill out the form and create the project (which have datetime), the format for my project always turn the same. It dosen't matter what I put in my input, it shows the same datetime.

My input type = 'datetime-local' and name = 'deadline'

My validation $attributes = request()->validate([ 'title' => ['required'], 'description' => ['required'], 'deadline' => ['date'] ]);

My migration

$table->dateTime('deadline')->nullable();

0 likes
1 reply
rodrigo.pedra's avatar

@mknylund It is nor clear from your description what you want to achieve.

I assume from usage of datetime-local that you expected that the date and time sent from the browser took somehow the local timezone into consideration. And as every datetime that is sent to your browser regardless of the timezone set in the browser is the same you might think there is a problem on how Laravel process it. Please forgive me if my interpretation is wrong.

If that is the case, unfortunately you are out of luck.

As you can read on the MDN page for datetime-local :

The control is intended to represent a local date and time, not necessarily the user's local date and time. In other words, an implementation should allow any valid combination of year, month, day, hour, and minute—even if such a combination is invalid in the user's local time zone (such as times within a daylight saving time spring-forward transition gap). Some mobile browsers (particularly on iOS) do not currently implement this correctly.

And also from the same page:

One thing the datetime-local input type doesn't provide is a way to set the time zone and/or locale of the date/time control. This was available in the datetime input type, but this type is now obsolete, having been removed from the spec. The main reasons why this was removed are a lack of implementation in browsers, and concerns over the user interface/experience. It is easier to just have a control (or controls) for setting the date/time and then deal with the locale in a separate control.

reference: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/datetime-local

Summarizing it, datetime-local only instructs browser to display a control that allows user to input date and time information.

You could argue that this is the role of datetime inputs, but datetime was deprecated in favor of dateime-local:

The HTML <input type="datetime"> was a control for entering a date and time (hour, minute, second, and fraction of a second) as well as a timezone. This feature has been removed from WHATWG HTML, and is no longer supported in browsers.

Instead, browsers are implementing (and developers are encouraged to use) <input type="datetime-local">.

Although local could suggest it would take user's timezone into account somehow, the specs only suggests browsers to display an way to input date and time that is familiar to users.

If you want to take the User's local timezone into account, you will need some JavaScript to send their timezone either by AJAX, or by filling a hidden input on your form.

You can find the browser's current set timezone with this snippet:

console.log(Intl.DateTimeFormat().resolvedOptions().timeZone)

reference: https://stackoverflow.com/a/44935836

Once you send both the datetime and the timezone to Laravel, it is your job to handle both if you want to save the date in a timezone agnostic format, such saving every datetime as a Unix timestamp or in UTC.

Good luck!

Please or to participate in this conversation.