How to get the excel date with PHPExcel?

1

Cordial Greeting I have created a class to perform massive loads with the help of PHPExcel, the information to load has columns in date format but it is impossible for me to capture them with the PHPExcel, in doing so it brings me numbers as if I were relieving a conversion in some way ...

Could someone explain to me what's owed that? and is there any way to fix it?

Query generated by PHPExcel

Image of the data (Example)

    
asked by Andros 10.07.2017 в 19:34
source

2 answers

2

You tried:

$cell = $excel->getActiveSheet()->getCell('B' . $i);
$InvDate= $cell->getValue();
if(PHPExcel_Shared_Date::isDateTime($cell)) {
     $InvDate = date($format, PHPExcel_Shared_Date::ExcelToPHP($InvDate)); 
}

Format Example:

$InvDate = date($format = "Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($InvDate)); 

maybe it will help you

    
answered by 10.07.2017 / 19:49
source
1

In case it is useful for someone I leave the code.

$fields_table = array(
    "nombre", "apellidos", "fecha_nac"
  );

  //inicializamos sql como un array
  $sql = array();

  //array con las letras de la cabecera de un archivo excel
  $letras = array(
    "A","B","C","D","E","F","G",
    "H","I","J","Q","L","M","N",
    "O","P","Q","R","S","T","U",
    "V","W","X","Y","Z"
  );

  //recorremos el excel y creamos un array para después insertarlo en la base de datos
  for($i = 1;$i <= $rows; $i++)
  {
    //ahora recorremos los campos del formulario para ir creando el array de forma dinámica
    for($z = 0; $z < count($fields_table); $z++)
    {
      $cell = $objPHPExcel->getActiveSheet()->getCell($letras[$z] . $i);
      $data= $cell->getValue();


      if(PHPExcel_Shared_Date::isDateTime($cell)) {
        $data = date($format = "Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($data)); 

      }

      $sql[$i][trim($fields_table[$z])] = $data;
    }
  }   

PDT: Thank you very much Francisco

    
answered by 10.07.2017 в 20:29