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

Lozza's avatar
Level 4

Date from form sometimes received in unexpected format

I am having issues with a Macbook Pro user when entering a date of birth on my UK based system.

The HTML code is input type="date" value="1999-01-31" class="border border-gray-200 rounded p-2 w-full" name="date_of_birth"

When I use the form, the date passed to the laravel backend is in this (expected) format: 1950-03-13

However, when the macbook pro user enters a date, it is received in this format: 13/03/1950

Is there something I should be doing to force the format of yyyy-mm-dd to be sent/received?

Thank you for any help :-) Lozza

0 likes
11 replies
LaryAI's avatar
Level 58

To ensure that the date is always received in the format "yyyy-mm-dd", you can use JavaScript to format the date before submitting the form. Here's an example of how you can achieve this:

<form id="myForm" action="/submit" method="POST">
  <input type="text" id="dob" name="dob">
  <button type="submit">Submit</button>
</form>

<script>
  document.getElementById('myForm').addEventListener('submit', function(event) {
    event.preventDefault(); // Prevent the form from submitting

    // Get the date input value
    var dobInput = document.getElementById('dob');
    var dobValue = dobInput.value;

    // Split the date into day, month, and year
    var parts = dobValue.split('/');
    var day = parts[0];
    var month = parts[1];
    var year = parts[2];

    // Format the date as yyyy-mm-dd
    var formattedDate = year + '-' + month + '-' + day;

    // Set the formatted date as the input value
    dobInput.value = formattedDate;

    // Submit the form
    this.submit();
  });
</script>

This JavaScript code listens for the form's submit event. When the event is triggered, it prevents the form from submitting, formats the date input value, sets the formatted date as the input value, and then submits the form programmatically.

Make sure to replace the action attribute of the form with the appropriate URL for your Laravel backend.

tisuchi's avatar

@lozza You can use createFromFormat() method to manipulate your date format.

For example:

$dateOfBirth = Carbon::createFromFormat('d/m/Y', $request->input('date_of_birth'))->format('Y-m-d');
1 like
ycsm's avatar

You could force laravel to change the date format:

Carbon::parse($request->date)->format('Y-m-d')
Lozza's avatar
Level 4

Thanks for your replies.

I don't think the createFromFormat solves my issue because I don't know what format is being sent/received. I am just trying Carbon::parse... I'll know soon whether it has stopped the error I have received however I wonder if it completely solves my problem because, what if it is sent in US format e.g. 01-05-1990 ... I wonder if it may result in confusion because the day and month.

ycsm's avatar

@Lozza You may be better off doing this client side as well to be doubly sure

Snapey's avatar

@Lozza when you say "mac user" I assume you mean someone using Safari?

Safari has had notoriously poor support for the native date input field, but has recently updated to better support the standard. This led to developers having to use third party date pickers so that they get the same results across all browsers, or they have resorted to separate fields for day month and year.

It may be that this user still has an old version?

The definitive source for compatibility these days seems to be MDN

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date

See this specifically

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date#handling_browser_support

1 like
Lozza's avatar
Level 4

Hi @snapey

Yes you are right, Safari. I am sure the issue is down to out of date Safari, since all the other macs at this firm seem to work ok. That said, I do need to deal with the issue, as obviously I have no control over whether the public users' browsers are up to date. I'll check out the document, cheers.

martinbean's avatar

@lozza There’s something else going on, because the date input is HTML spec-compliant, even in Safari. I’ve used Safari as my primary web browser for literally years now, and would have definitely noticed this.

I’ve even verified this in a Laravel project by creating a simple form:

<form action="{{ url('test') }}" method="post">
    <fieldset>
        <input type="date" name="date" id="date">
        <button type="submit">Submit</button>
    </fieldset>
</form>

And then submitting the data, and the value submitted did indeed come back in YYYY-MM-DD format in Safari:

Screenshot

kokoshneta's avatar

@martinbean It works fine now, but Safari didn’t suppose input type="date" until version 14.1, which was released in April 2021. Before that, it defaulted to a plain text field.

It’s perfectly possible to work around that with JavaScript (that’s been done for years and years), but in this case I think the issue is that the user is on a pre-14.1 version of Safari and typing in their birth date as dd/mm/yyyy. Since nothing has been done until now to prevent errors in browsers that don’t support the date type, it gets passed as is.

Lozza's avatar
Level 4

I don't have direct access to see what version of Safari is in use, but I'm pretty sure the problem is due to a pre-14.1 version of safari. I guess it would be very easy for me to check in laravel if the date is in the format dd/mm/yyyy or yyyy-mm-dd and process accordingly but what if the user has a US setup... could it send the date in mm/dd/yyyy format to throw another spanner in the works?

Snapey's avatar

@Lozza its not to do with the locale of the browser.

If the browser does not support the date input it will show a text input, then it comes down to what label you put on the field and the placeholder. In the MDN article I linked it shows how you can use an input regex pattern to ensure the user enters YYYY/MM/DD. Unfortunately it does not know if 1971/02/11 has the month and date in the right order, although the user should know that month goes in the middle if starting with the year.

Please or to participate in this conversation.