1
require_once 'PHPExcel.php';
require_once 'PHPExcel/IOFactory.php';

//Values
$path = getcwd(); 

        $latest_ctime = 0;
        $latest_filename = '';    

        $d = dir($path);
        while (false !== ($entry = $d->read())) {
        $filepath = "{$path}/{$entry}";
        // could do also other checks than just checking whether the entry is a file
        if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
          $latest_ctime = filectime($filepath);
         $latest_filename = $entry;
        }}

//Reading Excel File

$objPHPExcel = PHPExcel_IOFactory::load($latest_filename);

foreach ($objPHPExcel->getWorksheetIterator() as $worksheet){
     $worksheetTitle = $worksheet->getTitle();
     $highestRow = $worksheet->getHighestRow(); // e.g. 10 For all rows
     $highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
     $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
}

//Calculationg Columns
$nrColumns = ord($highestColumn) - 64;

//Displaying Sheet Details

echo "File name: " . $path;
echo '<br>';
echo "Sheet name: " . $worksheetTitle;
echo '<br>';
echo "No. of Columns: " . $nrColumns;
echo '<br>';
echo "No. of Rows: " . $highestRow;
echo '<br><br>';
$i=1;
$j=0;
for ($row = 2; $row <= $highestRow; ++ $row) {
$val=array();
for ($col = 0; $col < $highestColumnIndex; ++ $col) {
$cell = $worksheet->getCellByColumnAndRow($col, $row);
$val[] = $cell->getValue();
}
$var=$val[2];   //the date value is in the 3rd column
echo date('d/m/Y H:i:s', PHPExcel_Shared_Date::ExcelToPHP($var)); 
echo "<br>";

The problem i am facing is converting a int time value read from a excel file to mysql datetime format to be inserted into DB.My excel file has values like 19-09-2015 00:00:00 which when i am using the above code converts to 19-09-2015 02:00:00 which is 2 hours more than the value in the excel data,this is happening for all the date values, 2 hours extra seem to be getting added,how do i make it print the correct value.

Pramod S
  • 94
  • 11

1 Answers1

0

There is clearly something wrong with your tiers timezones. Check the timezones of PHP (you can start at corresponding PHP documentation) and MySQL, and make sure they match.

Evgeniy Chekan
  • 2,615
  • 1
  • 15
  • 23