The problem you suffer is because the strtotime()
function is returning false
when converting to chain date of text fails.
When this false
is passed as the second parameter of date()
it is returning the date January 1, 1970 00:00: 00 UTC in your time slot because false
is evaluated as the UNIX timestamp 0
(zero).
If you are in the American continent (you have a negative time slot) then it will result in the day before, on December 31, 1969.
Examples:
<?php
$timezones = [
'Europe/Madrid',
'America/Montevideo',
];
foreach($timezones as $timezone) {
date_default_timezone_set($timezone);
echo $timezone, ': ', date('Y-m-d', false), PHP_EOL;
}
Result:
$ php pruebas.php
Europe/Madrid: 1970-01-01
America/Montevideo: 1969-12-31
If you want to detect that the conversion was wrong then you should check the output of strtotime()
:
<?php
if (isset($_POST['Fecha'])) {
$Fecha1 = $_POST['Fecha'];
/* Almacenamos la marca de tiempo (timestamp) como variable */
$timestamp = strtotime(str_replace('/', '-', $Fecha1));
/* Comprobamos su contenido para saber si se convirtió la fecha correctamente */
if ($timestamp !== false) {
/* En caso positivo generamos la fecha */
$Fecha2 = date("Y-m-d", $timestamp);
} else {
/* En caso negativo hacemos algo con la cadena o lanzamos un error */
$Fecha2 = 'ERROR EN FECHA';
}
} else {
$Fecha2 = "";
}