I'm trying to store some dates in my datebase (MySQL). But I got a strange conversion error: This is my piece of PHP code:
$fechanac=mysql_real_escape_string(($_POST["fechanac"]));
echo "<h1>{$fechanac}</h1>";
$fechanac=date('Y-m-d', strtotime(str_replace('/', '-', $fechanac)));
echo "<h1>{$fechanac}</h1>";exit();
See the following three example, trying with (01/01/1900, 01/01/1901 and 01/01/1902).
OUTPUT:
01/01/1900
1970-01-01
01/01/1901
1970-01-01
01/01/1902
1902-01-01
Somebody know why happens this? And how to fix it? I need to insert in my DB, possible dates of living persons. Thanks for reading.
Date stored!
$fechanac=mysql_real_escape_string(($_POST["fechanac"]));
echo "<h1>{$fechanac}</h1>";
$datearray=explode("/",$fechanac);
$fechanac="{$datearray[2]}-{$datearray[1]}-{$datearray[0]}";
I know it's and old post, but maybe this late answer can help others:
The DateTime class correctly manages dates older than 1970. So forget the use of date()
and evolve to DateTime
:
$fechanac=mysql_real_escape_string(($_POST["fechanac"]));
echo "<h1>{$fechanac}</h1>";
$fechanac=(new DateTime($fechanac))->format('Y-m-d');
echo "<h1>{$fechanac}</h1>";exit();