I am working on a Laravel project where I need to add a date field in the user profile section(a form). The date should:
- Be displayed in the format DD / MMM / YYYY (e.g., 25 / Jun / 2024).
- Allow users to edit/select the date.
- Send the date to the backend in a regular YYYY-MM-DD format when the form is submitted.
- Fetch the saved date from the backend and display it in the same DD / MMM / YYYY format when the page is visited.
I tried using <input type="date">
, but it displays the date as 25/06/2025
, which isn’t the desired format. I also tried following approach:
`
{!! Form::select(‘day’, range(1, 31), null, [‘placeholder’ => ‘Day’, ‘class’ => ‘form-control’]) !!}
{!! Form::select(‘month’, [’01’ => ‘Jan’, ’02’ => ‘Feb’, ’03’ => ‘Mar’, …, ’12’ => ‘Dec’], null, [‘placeholder’ => ‘Month’, ‘class’ => ‘form-control’]) !!}
{!! Form::select(‘year’, range(date(‘Y’), 1900), null, [‘placeholder’ => ‘Year’, ‘class’ => ‘form-control’]) !!}
`
The JavaScript code for aligning the number of days with the selected month and year, feels overly complex for such a simple requirement.
**Is there an easier way to:
**
Display the date in DD / MMM / YYYY format.
Allow users to edit/select the date.
Fetch and display the saved date(dd-mm-yyyy) in the correct format(dd-mmm-yyyy or 16-05-2025) when the page is visited.
2
Answers
You can approach it by collecting the date in its usual format from the $request from the frontend and then use Carbon object to parse the date to the dd/mm/YYYY format in your backend. You will need to include the Carbon package at the top of your class though before being able to use it
Solution Using Flatpickr/Carbon (it’s a JavaScript Date Picker)
Let’s say you’d like to work on your birthday date.
You can do something like this using Laravel
In your webpage
Here’s how to handle the date in the PHP controller
This is a very simple example but you can customize it to your liking in your project using JavaScript code for aligning the number of days.