0

I have two pages, one where the php instructions are enclosed together with the query, the other the form that in the action recalls the first page.

I have a field where there is a datepicker, this field is called data_scadenza, on the first page I value this field:

$data_scadenza = date('Y-m-d',strtotime($_POST['data_scadenza'])); 

query:

    $updateSQL = sprintf("UPDATE Cassetto_documentale SET nome_file_cassetto=%s, data_scadenza_cassetto=%s, data_revisione=%s, ok_doc_direttore=%s, id_caricato_pdf=%s, data_caricato_pdf=%s, ID_caricato_da_cassetto=%s WHERE ID_doc_casstto=%s",

                           GetSQLValueString($data_scadenza, "date"),
                           GetSQLValueString($data_revisione, "date"),                                           
                           GetSQLValueString($ok_direttore, "text"),
                           GetSQLValueString($_COOKIE['cod_op'], "int"),
                           GetSQLValueString($d_oggi, "date"),
                           GetSQLValueString($ID_utente_convalida, "int"),
                           GetSQLValueString($id_doc, "int"));

then I execute the explode, and implement the form input:

$d_scad = $row_doc_cassetto['data_scadenza_cassetto'];

<?php
if ($d_scad == ""){
    //$data_scadenza = date("d/m/Y");
}else{
    $d_scad = explode('-', $d_scad);
    $yy_scad = $d_scad[0];
    $mm_scad = $d_scad[1];
    $gg_scad = $d_scad[2];
    $d_scad = $gg_scad."/".$mm_scad."/".$yy_scad;
}
?>


if ($d_scad == ""){
    $data_scadenza = date("d/m/Y");
}else{
    $d_scad = $d_scad;  
}
?>

<input type="text" name="data_scadenza" class="form-control" id="datetimepicker1" pattern="(0[1-9]|1[0-9]|2[0-9]|3[01])[- /.](0[1-9]|1[012])[- /.][0-9]{4}" value="<?php echo $d_scad;?>" >

The problem is the following, if I select the date with the datepicker, I get a value 01-01-1970, if instead I insert it manually everything is recorded perfectly, I would like the date with the selection of the datepicker, to be registered correctly in the database, as I can do?

treyBake
  • 6,440
  • 6
  • 26
  • 57

1 Answers1

2

If I read your question correct this is what you are trying to do:

$_POST['data_scadenza'] = "15/04/2019";

echo $data_scadenza = date('Y-m-d',strtotime($_POST['data_scadenza']));
//1970-01-01

This is because you can't use dd/mm/yyyy format with strtotime.

Either change the input to something like

$_POST['data_scadenza'] = "15-04-2019";

echo $data_scadenza = date('Y-m-d',strtotime($_POST['data_scadenza']));

Which can easily be done with str_replace.

$_POST['data_scadenza'] = "15/04/2019";

echo $data_scadenza = date('Y-m-d',strtotime(str_replace("/", "-", $_POST['data_scadenza'])));

Or use date_create_from_format() to parse the input date.

Andreas
  • 23,610
  • 6
  • 30
  • 62