Input date format yyyy-mm-dd

Žan Anđić
Žan Anđić used Ask the Experts™
on
Hi,

I have little problem with date picker in html input field.
When I choose date from picker it show in this format: dd.mm.yyyy 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 dd.mm.yyyy

there is html:

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

Open in new window




EDIT:

I am fix that using date("d.m.Y",strtotime($_POST['datum_rodjenja']));
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
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.

Author

Commented:
Hi,

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:

$date_of_birth=$_POST['date_birth'];

with this:

$date_of_birth=("d.m.Y",strtotime($_POST['datum_rodjenja']));
Ryan ChongSoftware Team Lead
Commented:
>>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.
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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

Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Is there a question in your last post?

Author

Commented:
Changed to DATE, displaying like I wrote on my previous commentar

Thank you Julian and Ryan
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You are welcome.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial