lundi 29 juin 2015

Problems with PHP date format

I decided to make this questions because something very rare is happening with my project!

I will explain:

I have this html code:

<input type="text" name="dob" value="<?php echo $rows['dob']; ?>"></input>

the value of this field is for example: 12-24-1962 because I convert the date that exist in the SQL database with this code:

$sql = "select DATE_FORMAT(dob, '%m-%d-%Y') AS dob,   .....

So later maybe the user edit the DOB (date of birth) field and I update this value in the database with this php code:

$dob_0 = DateTime::createFromFormat('m-d-Y', $_POST['dob']);
$dob   = $dob_0->format('Y-m-d');

$sql = "UPDATE $tbl_name SET dob = '$dob', .......

So with this the date arrive to database with this format: YYYY-MM-DD

My doubt is:

Can pass weeks and nothing happen but sometimes (random) I dont know why this exact code produce a fatal error like this:

Fatal Error: Call to a member function format() on a non-object in...

And the line of the error is this:

$dob_0 = DateTime::createFromFormat('m-d-Y', $_POST['dob']);

What is happening? Why does the code work for some time and break later?

Aucun commentaire :

Enregistrer un commentaire