strtotime returns 1969-12-31

2

When the field Fecha is empty the variable $Fecha2 returns 1969-12-31 . When it's complete it works perfect.

I leave this portion of code:

<?php
if(isset($_POST['Fecha']))
{
$Fecha1=$_POST['Fecha'];
$Fecha2= date("Y-m-d", strtotime(str_replace('/', '-', 
$Fecha1))); 
}else{
$Fecha2="";
}
    
asked by pointup 26.11.2018 в 14:56
source

2 answers

4

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 = "";
}
    
answered by 26.11.2018 / 15:02
source
1

If you are interested in writing a controlled code I propose the following:

  • Evaluate the POST with a ternary operator
  • Build the date using DateTime , establishing a control of possible erroneous dates

For example:

<?php
    $fechaPost=( empty($_POST['Fecha']) ) ? NULL : $_POST['Fecha'];
    $mFecha= NULL;
    if ($fechaPost)
    {
        try 
        {
            $mFecha=new DateTime($fechaPost);
        } 
        catch (Exception $e) 
        {
            echo $e->getMessage().PHP_EOL;
        }
    }
    /*Probando el resultado*/
    var_dump($mFecha);
?>

The code assigns the final variable $mFecha the value NULL by default, and will only change when there is a valid date.

Some tests:

$_POST=["Fecha"=>""];  

Exit:

NULL

With a wrong date.

$_POST=["Fecha"=>"21"];

Exit:

In this case, capture the error:

DateTime::__construct(): Failed to parse time string (21) at position 0 (2): Unexpected character
NULL

With a correct date:

$_POST=["Fecha"=>"21-10-2018"];

Exit:

A valid DateTime object:

object(DateTime)#1 (3) {
  ["date"]=>
  string(26) "2018-10-21 00:00:00.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(13) "Europe/Berlin"
}
    
answered by 26.11.2018 в 15:23