0

I know this question has been asked before but none of the solutions is working for me, I have got a form in which the user enters the date of birth, but for reason it is set to mm/dd/yyyy and I want to change it to dd/mm/yyyy. As soon as i enter the date like 19/12/1992 it prompts invalid date format.

The code in the form is :

<label for="name">Date of Birth (mm/dd/yyyy) <font color="red"> *</font></label> <input type="text" name="DOB" value="<?php echo $info['DOB']; ?>" onfocus="$(this).removeClass('date required')" onchange="$(this).addClass('date required')" required /> 

And the code in the function after POST is:

$DOB = mysql_real_escape_string($_POST['DOB']); 

3 Answers 3

1

EDITED: use this:

1)

 $DOB = mysql_real_escape_string($_POST['DOB']); $newDate = date("d/m/Y", strtotime($DOB)); 

OR

2) $newDate = DateTime::createFromFormat('m/d/Y', $DOB)->format('d/m/Y');

Sign up to request clarification or add additional context in comments.

4 Comments

thanks a lot, but now it is not saving the value, it is saving a date 01/01/1970
may be the format you are trying to conver is unsuported by strtotime.
@user3297381 can you try the second option and see.
0

Like they all said do this

 $DOB = mysql_real_escape_string($_POST['DOB']); $newDate = date("d/m/Y", strtotime($DOB)); 

Then at the other side where you are either inserting or updating the database change the date back to mysql standard format like this:

$DOB_DB = mysql_real_escape_string($_POST['DOB']); $DOB_DB = date("m/d/Y", strtotime($DOB_DB)); 

1 Comment

It worked this way by reverting the format back, thanks a lot !!
0

You could use strtotime:

$oldDOB = mysql_real_escape_string($_POST['DOB']); $DOB = date("d/m/Y", strtotime($oldDOB)); 

1 Comment

thanks a lot, but now it is not saving the value, it is saving a date 01/01/1970

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.