Finding Date Value

Sep 16, 2010 at 10:57 AM

I have read one of the previous posts regarding dates, but my problem has not yet been solved. The scenario is as follow:

  1. My clients can upload a file in either .csv, .xls or .xlsx format.
  2. Any column can contain a date vale
  3. The date value si then upload as a numeric string

Using this code:

<?php
error_reporting(E_ALL);
$fnme = $_REQUEST['fnme'];
$rid = $_REQUEST['rid'];
$fst = $_REQUEST['fst'];
require_once '../phpex/Classes/PHPExcel/IOFactory.php';
$objPHPExcel = PHPExcel_IOFactory::load("docs/".$fnme);
$objWorksheet = $objPHPExcel->getActiveSheet();
echo '<table id="uplTbl" class="table">' ;
foreach ($objWorksheet->getRowIterator() as $row) {
	echo '<tr>' ;
	
	$cellIterator = $row->getCellIterator();
	$cellIterator->setIterateOnlyExistingCells(false);
	$c=0;
	foreach ($cellIterator as $cell) {
			
		if (PHPExcel_Shared_Date::isDateTime($cell->getValue())) {
			echo '<td class="td">' . date("d M Y",PHPExcel_Shared_Date::ExcelToPHP($cell->getValue())) . '</td>' ;
		}else{
			echo '<td class="td">' . $cell->getValue() . '</td>' ;
		}
	}
	
	echo '</tr>' ;
}
echo '</table>' ;
?>

I get the following error:
	

Catchable fatal error

string given, called in /home/syncohum/public_html/sms/template/getfile.php on line 18 and defined in

/home/syncohum/public_html/sms/phpex/Classes/PHPExcel/Shared/Date.php on line 207

 

(Line 18 refers to

if (PHPExcel_Shared_Date::isDateTime($cell->getValue())) {)

: Argument 1 passed to PHPExcel_Shared_Date::isDateTime() must be an instance of PHPExcel_Cell,

Any assistance will greatly be appreciated.
NicOos
Sep 16, 2010 at 10:59 AM

The last bit got a bit scrambled....

The error is:

Catchable fatal error: Argument 1 passed to PHPExcel_Shared_Date::isDateTime() must be an instance of PHPExcel_Cell, string given, called in /home/syncohum/public_html/sms/template/getfile.php on line 18 and defined in /home/syncohum/public_html/sms/phpex/Classes/PHPExcel/Shared/Date.php on line

 Line 18 refers to:

if (PHPExcel_Shared_Date::isDateTime($cell->getValue())) {

Coordinator
Sep 16, 2010 at 11:22 AM
Edited Sep 16, 2010 at 11:23 AM

The error pretty much says it all:

Argument 1 passed to PHPExcel_Shared_Date::isDateTime() must be an instance of PHPExcel_Cell, string given

You're calling isDateTime() explicitly passing the value from the cell

if (PHPExcel_Shared_Date::isDateTime($cell->getValue)) {

When you should be passing the cell rather than its value

if (PHPExcel_Shared_Date::isDateTime($cell)) {

The only way that Excel differentiates between numbers and dates is the number format mask, which isn't anything to do with the value in the cell, but with the formatting of the cell... so we need to be able to read the cell formatting to identify a date. To obtain the formatting for isDateTime() to determine if the format mask is a date/time format or not, we need the cell itself

 

Note that if the file could be a CSV, you should probably be using the Advanced Value Binder so that the loader can correctly set the format mask for date values

Sep 16, 2010 at 11:50 AM

Thanks, it worked like a charm...