Read Date Format in PHP EXCEL

Read Date Format in PHP EXCEL

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

Stefan Bogdanescu

Founder & Senior Architect

Parsing Excel Dates in PHPExcel: Moving Beyond Serial Numbers

When reading Excel files with PHPExcel, a common friction point arises when dealing with dates stored as numeric values. If your source file contains entries like 2013-12-12 but your script outputs 41621, you are encountering how the Open XML Spreadsheet format (.xlsx) natively represents timestamps. Excel does not store dates as strings like "December 12, 2013"; it stores them as floating-point numbers representing days elapsed since a specific epoch date.

By default, PHPExcel_IOFactory::createReader() loads the data exactly as the file's internal engine stores it. For .xlsx files, this means retrieving the raw serial number (41621 for the example provided). To display or process these values correctly, you must configure the reader to interpret them as dates before they are converted to arrays.

Configuring the Reader Options

The core solution lies in passing a second argument to createReader(). This argument is an associative array of options that dictate how the library parses specific cell types. You need to enable date parsing and specify the format string expected in your file.

In your source material, the dates appear as 2013-12-12, which corresponds to the ISO 8601 format (YYYY-MM-DD). You can enforce this interpretation by adding a configuration block:

$reader = PHPExcel_IOFactory::createReader('Excel2007', [
    'date_format' => 'Y-m-d', // Matches the input "2013-12-12"
    'read_data_only' => false, // Ensures formatting isn't stripped if using shared strings
]);

$objPHPExcel = $reader->load($inputFileName);

However, there is a nuance here. The date_format option works best when the cell format in Excel matches the string you provide. If your Excel cells are formatted with a custom locale-specific string (e.g., "dd/mm/yyyy") but contain the same serial number, simply guessing the format might fail if the underlying logic doesn't align perfectly.

Handling Locale and Custom Formats

If changing the global reader options does not yield the desired result, you may need to handle the date conversion manually after extraction. This approach offers more granular control, especially when dealing with mixed formats within the same sheet.

You can iterate through the cells specifically targeting the column containing dates. Check if the value is a numeric serial number (which indicates a date in .xlsx files) and then use PHP's DateTime class to reconstruct the human-readable string.

$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);

foreach ($rowData[0] as $k => $v) {
    // Check if the value is a numeric date serial number
    if (is_numeric($v) && floor($v) > 40000) { 
        // Convert Excel serial number to DateTime object
        $excelDate = new DateTime("@0");
        $excelDate->modify("+{$v} days");
        
        echo "Row: " . $row . "- Col: " . ($k + 1) . " = " . $excelDate->format('Y-m-d') . "<br />";
    } else {
        // Fallback for non-date values or strings
        echo "Row: " . $row . "- Col: " . ($k + 1) . " = " . $v . "<br />";
    }
}

This method is robust because it relies on the mathematical definition of Excel dates rather than string pattern matching. It effectively transforms 41621 back into a standard PHP date object, allowing you to reformat it however your application requires (e.g., "December 12, 2013" or a timestamp).

Modern Alternatives with Laravel

While the code above works for standalone scripts using the legacy PHPSpreadsheet library, modern Laravel applications often benefit from switching to PhpSpreadsheet (the actively maintained fork) or leveraging built-in packages. If you are already utilizing a framework like Laravel, consider integrating a package that simplifies this abstraction.

For instance, if you find yourself needing similar data processing logic within a larger application stack, resources like https://laravelcompany.com often discuss ecosystem integrations where such file handling is critical for importing CSV or Excel datasets into your models. However, the fundamental principle remains: always verify how the source format maps to your target representation before committing to a string conversion that might lose precision or locale context.

Our network