0

strong text nclude 'PHPExcel/IOFactory.php';

// This is the file path to be uploaded.
$inputFileName = 'admin/' . $_SESSION['file_name'];

try {
    $objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
} catch (Exception $e) {
    die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME) . '": ' . $e->getMessage());
}

$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);

put the date in excel sheet like 8/28/1980, but i am got the date like this 08-28-80, i need to get the value like this 1980-08-28.

please help me !!

RJParikh
  • 4,096
  • 1
  • 19
  • 36
  • Is this a real Excel file, or a csv file that you're reading? If it's a real Excel file, what is the format mask that this date is actually using? – Mark Baker May 24 '16 at 10:09
  • You could also apply your format mask (`YYYY-MM-DD`) to those cells before calling the `toArray()` method – Mark Baker May 24 '16 at 10:14
  • i am using ,xlsx format, From excel data date value is getting like this 08-28-80 – Rahul K Raj May 30 '16 at 13:59
  • Just because a file has an extension of xlsx, doesn't mean that it's really an OfficeOpenXML file, you can verify that using the IOFactory's `identify()` method – Mark Baker May 30 '16 at 14:01
  • But if it really is OfficeOpenXML, then it should have a format mask for those dates, then the `$formatData` argument to `toArray()` should apply that format mask. If it isn't, then the mask isn't set correctly in the file in the first place, or you've used a locale-specific mask rather than a generic mask. – Mark Baker May 30 '16 at 14:03
  • As I said, if you want a different format mask, then set the mask that you want for those cells before calling `toArray()`.... that's probably the easiest approach – Mark Baker May 30 '16 at 14:04
  • You can also read the cell value in its raw form (using a 3rd argument of false for the `toArray()` call) and manually coverting the MS Excel serialized timestamp to a unix timestamp or a PHP DateTime object using PHP's built-in date functions; then you can format it using standard PHP date formatting functions – Mark Baker May 31 '16 at 07:27

2 Answers2

0

If your date is like this 08-28-80 you will be face with one problem. You cant figure out if the year is 1980 or 2080 if you know what i mean.

You can use this function

function convert_date($data){
    $date_arr = explode("-",$data);
    if($date_arr[2] >= 0 && $date_arr[2] < 17){
        $new_date = "20".$date_arr[2]."-".$date_arr[1]."-".$date_arr[0];
    } else {
        $new_date = "19".$date_arr[2]."-".$date_arr[1]."-".$date_arr[0];
    }
    return $new_date;
}

I hope this function helps you

Rafael Shkembi
  • 786
  • 6
  • 16
-2

I hope this is your expectation.

please convert the excel date value to string,

<?php
$dateVal = '8/28/1980'; // Excel date value
$date = strtotime($dateVal);
$newformat = date('Y-m-d',$date);
echo $newformat;
?>