Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPExcel, Date being parsed incorrectly

Currently, I'm making a program for my job that uses PHPexcel to basically take all of the data from an excel sheet, make a new excel sheet and format it to text, and transfer all of the data from the old excel sheet.

My php has to be able to take in the values from the old excel sheet and parse them correctly, especially dates/numbers/etc.

Everything is working fine except for the dates. For some reason when I get the formatted value of the cell; it does not match the date format in the first sheet. Specifically a value like this: 12/31/2099 . For some reason, it reads the formatcode incorrectly as mm-dd-yy and outputs 12-31-99. This is extremely frustrating and I've scoured half of the internet for an answer. Help please; I'm not sure what the issue is! Code:

<?php
$spreadSheet=$_FILES["filebrowser"]["tmp_name"];
/** PHPExcel_IOFactory */
require_once 'Classes/PHPExcel/IOFactory.php';
require_once 'Classes/PHPExcel/Shared/Date.php';
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
//Create temporary sheet from uploaded file data
$objPHPExcel = $objReader->load($spreadSheet);
$sheet = $objPHPExcel->getActiveSheet();
$sheet->setTitle("Original Data");
$data=$sheet->getCell("D9")->getFormattedValue();

var_dump($data);
exit;?>

D9 is a cell with DATE format and a value of 12/31/2099. This code prints 12-31-99...

like image 890
Joseph Amato Avatar asked Jul 11 '13 19:07

Joseph Amato


3 Answers

Using PHPExcel v1.7.9

Open file "PHPExcel\Style\NumberFormat.php"

Go to Line 278:

self::$_builtInFormats[22] = 'm/d/yy h:mm'; 

replace with

self::$_builtInFormats[22] = 'dd-mm-yyyy hh:mm';

or whatever format you want.

I can't find an efficient way to change it on the fly and I have no reason to ever format the date in any way other than this so this solution works for me.

like image 176
steve Avatar answered Oct 06 '22 00:10

steve


Using v1.7.8 and assuming Excel2007 aka .xlsx file

$a1Cell = $sheet->getCell('A1');
$a1CellFormattedValue = trim($a1Cell->getFormattedValue());
if (!empty($a1CellFormattedValue)) {
    $dateInTimestampValue = PHPExcel_Shared_Date::ExcelToPHP($a1Cell->getValue());
}

Let me explain each line.

  • line 1 is to get the Cell object
  • line 2 and the if block is so that we only perform for non-empty cells
  • line 4 is how we transform the value inside the cell into unix timestamp.

You're left with deciding what date formats to write the value into the destination excel file. My suggestion is that you decide with the stakeholders that ALL dates in destination excel files will follow a particular format.

This is the cleanest way I can think of.

P.S. If you have an empty cell, and you try to do the PHPExcel_Shared_Date::ExcelToPHP on the cell value, you will get the current date.

UPDATE

Just realized that your comments mention you cannot arbitrarily decide the format. Sorry about that. If I come across something that helps, I will update this answer.

like image 32
Kim Stacks Avatar answered Oct 05 '22 23:10

Kim Stacks


I just came across your question. if you know the format in which excel is saving date, then you can use the PHP class DateTime as an intermediate date representation

$data=$sheet->getCell("D9")->getFormattedValue();
$date = DateTime::createFromFormat ("m-d-y" , $data);
$string_date = $date->format($format);

where $format is any format you want

like image 44
Nashwan Avatar answered Oct 05 '22 23:10

Nashwan