how to get date from excel using PHPExcel library

how to get date from excel using PHPExcel library

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Decoding Excel Dates in PHPExcel: Beyond Simple String Extraction

When working with spreadsheet data in PHP, retrieving a date from an Excel file is a common requirement. However, developers often encounter a specific pitfall: they retrieve a numeric value or a formatted string instead of a usable DateTime object. The issue usually stems from misunderstanding how Excel represents dates internally versus how the PHPExcel library exposes them by default.

In your current implementation, you are using $cell->getValue(). This method returns the raw cell content based on its formatting. For example, if an Excel cell is formatted to display "01/01/2023", getValue() will return that string. Even if the underlying data is a serial number (the number of days since January 1, 1900), the library might apply the cell's number format before returning the value. This results in getting a string or a serialized date representation rather than the numeric epoch or a proper date object you can manipulate in PHP.

To fix this, you need to explicitly request the raw value without formatting. In PHPExcel (and its successor PHPSpreadsheet), there is a dedicated method for this: getCalculatedValue(). Unlike getValue(), which respects cell styles and number formats, getCalculatedValue() returns the internal representation of the data. For date cells, Excel stores dates as serial numbers starting from 1 (representing January 1, 1900).

Here is how you should adjust your code snippet:

// Get the raw calculated value (the Excel serial number)
$rawValue = $excel->getActiveSheet()->getCell('B' . $i)->getCalculatedValue();

// Check if it is indeed a date serial number before converting
if ($rawValue !== null && $rawValue > 1900) {
    // Convert the Excel serial number to a PHP DateTime object
    // Note: Excel treats 1900 as year 1. We must account for this offset 
    // or use a custom converter, but often simple arithmetic works:
    
    // Create a base date (Jan 1, 1900)
    $baseDate = new DateTime('1899-12-30');
    
    // Add the number of days represented by the serial number
    // Excel serial numbers are days since Dec 30, 1899 effectively for PHP conversion
    $dateObj = clone $baseDate;
    $dateObj->modify("+$rawValue days");
    
    echo $dateObj->format('Y-m-d');
}

Alternatively, if you prefer using PHPSpreadsheet (the modern successor to PHPExcel), the approach is similar but utilizes the getCoordinate() and getStyle() logic differently. You must ensure that you are not inadvertently applying a number format mask that converts the serial number into a string like "2023-01-01".

A common mistake involves using PHPExcel_Calculation_DateFunctions::getDateFromExcelSerial() if you are on an older version, but manual conversion is often more reliable across versions. The core logic remains: identify the numeric offset and map it to a calendar date.

If your data comes from a modern Excel file where dates might be stored as strings (e.g., "mm/dd/yyyy") rather than serial numbers, getCalculatedValue() will return that string directly. In that scenario, you must parse the string using PHP's DateTime constructor:

$cell = $excel->getActiveSheet()->getCell('B' . $i);
$value = $cell->getCalculatedValue();

if (is_numeric($value)) {
    // Handle as Excel serial number
    $dateObj = new DateTime('@' . ($value - 25569) * 86400); 
} else {
    // Handle as string, assuming standard format
    $dateObj = new DateTime($value);
}

The choice between handling serial numbers and strings depends entirely on how the source Excel file was created. If the column is formatted as "Date", it is usually a serial number. If it is formatted as "Text", it is a string. Always inspect the cell type using $cell->getStyle()->getNumberFormat() or by checking if is_numeric() returns true for the calculated value.

For projects requiring robust spreadsheet handling, consider migrating to PHPSpreadsheet, which offers better support for complex formatting and data types. While PHPExcel is still functional, it lacks many modern Excel features. If you are building a larger enterprise application, integrating these logic checks into a dedicated utility class ensures consistent date handling across your entire codebase.

Our network