Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPExcel: how to set date format for a cell

I need to save a date to Excel file, it must be output in format "dd/mm/yyyy" (or the local date format of the user), and to be treated as a date so a column of them could be sorted correctly.

Here is the code:

<?php
include_once("../PHPExcel/Classes/PHPExcel.php");
date_default_timezone_set('Europe/London');
$objPHPExcel = new PHPExcel();
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;
PHPExcel_Settings::setCacheStorageMethod($cacheMethod);

$objPHPExcel = new PHPExcel();
$sheet = $objPHPExcel->getActiveSheet();
PHPExcel_Shared_Font::setAutoSizeMethod(PHPExcel_Shared_Font::AUTOSIZE_METHOD_EXACT);

//I didn't find dd/mm/yyyy format, so I used yyyy-mm-dd
$sheet->setCellValueByColumnAndRow(0, 1, "2014-10-16");
$sheet->getStyleByColumnAndRow(0, 1)
    ->getNumberFormat()->setFormatCode(
        PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2
);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save("test.xlsx");

It creates a file, but instead of my local date format I see: "2014-10-16" and the format of the cell is "All formats" -> "yyyy-mm-dd". I wanted it to be parsed and output in my local date format.

I looked into the source code of PHPExcel/Classes/PHPExcel/Style/NumberFormat.php, and found many date formats:

const FORMAT_DATE_YYYYMMDD2 = 'yyyy-mm-dd';
const FORMAT_DATE_YYYYMMDD = 'yy-mm-dd';
const FORMAT_DATE_DDMMYYYY = 'dd/mm/yy';
const FORMAT_DATE_DMYSLASH = 'd/m/y';
const FORMAT_DATE_DMYMINUS = 'd-m-y';
...

But I am not sure what to use. How can I achieve the desired goal?

like image 591
user4035 Avatar asked Aug 13 '16 13:08

user4035


People also ask

How do I change date format from DD mm to YYYY?

If you want more date formatting options, either select More Number Formats from the drop-down list or click the Dialog Box Launcher next to Number. This will open a familiar Format Cells dialog and you can change date format there. Tip. If you want to quickly set date format in Excel to dd-mmm-yy, press Ctrl+Shift+#.

How do I change the date format from text to dd mm yyyy in Excel?

For this, simply pick Date in the Number Format box on the Home tab. To apply a format other than default, then select the cells with serial numbers and press Ctrl+1 to open the Format Cells dialog. On the Number tab, choose Date, select the desired date format under Type and click OK. Yep, it's that easy!

How do I format the date in a cell in Excel?

Select the cells you want to format. Press CTRL+1. In the Format Cells box, click the Number tab. In the Category list, click Date, and then choose a date format you want in Type.


2 Answers

This code generates 4 cells in date format.

<?php
include_once("../PHPExcel/Classes/PHPExcel.php");
date_default_timezone_set('Europe/London');
$objPHPExcel = new PHPExcel();
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;
PHPExcel_Settings::setCacheStorageMethod($cacheMethod);

$objPHPExcel = new PHPExcel();
$sheet = $objPHPExcel->getActiveSheet();
PHPExcel_Shared_Font::setAutoSizeMethod(PHPExcel_Shared_Font::AUTOSIZE_METHOD_EXACT);

$format = 'dd/mm/yyyy';
for ($i = 1; $i < 5; ++$i)
{
    $date = new DateTime('2016-12-0'.$i);
    $sheet->setCellValueByColumnAndRow(0, $i, 
                                       PHPExcel_Shared_Date::PHPToExcel( $date ));

    $sheet->getStyleByColumnAndRow(0, $i)
        ->getNumberFormat()->setFormatCode($format);
}

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save("test.xlsx");
like image 58
user4035 Avatar answered Sep 23 '22 14:09

user4035


$sheet->setCellValueByColumnAndRow(0, 1, "2014-10-16");

Sets a string value in the cell, not a date. Just because you interpret that as a date, doesn't mean that computer programs automagically interpret it as a date.

Look at the date Examples in the PHPExcel Documentation and Examples, and you'll see that you need to set the cell value to a MS Excel serialized timestamp (a float value of the number of days since 1st January 1900). You can use the PHPExcel functions like PHPExcel_Shared_Date::PHPToExcel() to convert human dates/PHP DateTime objects/Unix timestamps to MS Excel Serialized timestamps.

$sheet->setCellValueByColumnAndRow(0, 1, PHPExcel_Shared_Date::PHPToExcel( '2014-10-16' ));

Once you've stored the value as a timestamp, you can then apply whatever date format mask you want to that cell to get your desired formatting

like image 36
Mark Baker Avatar answered Sep 23 '22 14:09

Mark Baker