Input date format yyyy-mm-dd


I have little problem with date picker in html input field.
When I choose date from picker it show in this format: but when that record save to database MySQL shows like this:
yyyy-mm-dd , what is the best way to make this working, to write in database date in this format

there is html:

<label>Date of birth:</label>
<input type="date"  class="form-control" name="date_birth" required />

Open in new window


I am fix that using date("d.m.Y",strtotime($_POST['datum_rodjenja']));
Žan AnđićAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Is your date field in the database created as a date type or a varchar / char type?

If you are saving to a database with a field that has the date type then save it in the format the database expects (example: yyyy-mm-dd).

When you retrieve the value and you need it to appear in a specific format you can do so by re-formatting the date
$database_date = // set db date here
$display_date = date('d.m.Y', strtotime($database_date));

Open in new window

The internal representation of the date is how the data is stored in the MySQL database you should not worry about what this looks like.

If you are storing your dates in a char / varchar field then that is different - consider changing to using a date type instead and then do as above.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Žan AnđićAuthor Commented:

Thank you for answer,

I am storing dates in a varchar field. I have date picker, and in date picker date is showed well. And I replace just this:


with this:

Ryan ChongSoftware Team LeadCommented:
>>I am storing dates in a varchar field

in general, dates should be stored as dates. unless there are special requirements why it's stored as chars.

as Julian also pointed out, consider to store the values in date data type to increase the data integrity.
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Žan AnđićAuthor Commented:
I changed to DATE, and when showing i put

$database_date = // set db date here
$display_date = date('d.m.Y', strtotime($database_date));

Open in new window

Julian HansenCommented:
Is there a question in your last post?
Žan AnđićAuthor Commented:
Changed to DATE, displaying like I wrote on my previous commentar

Thank you Julian and Ryan
Julian HansenCommented:
You are welcome.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.