In PHP Spreadsheet, the date or time values are stored in an excel sheet in the form of timestamp which is a floating point value. So in order to store date/time in a human-readable format, need to calculate the correct excel timestamp and finally set a number format mask.
Example:
<?php
require_once ( 'vendor/autoload.php' );
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet ->getActiveSheet();
$spreadsheet ->getActiveSheet()->getStyle( 'A1' )
->getNumberFormat()
->setFormatCode(
\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DATETIME
);
$dateTime = time();
$excelDateValue = \PhpOffice\PhpSpreadsheet\Shared\ Date ::PHPToExcel(
$dateTime );
$sheet ->setCellValue( 'A1' , $excelDateValue );
$writer = new Xlsx( $spreadsheet );
$writer ->save( 'gfgdate.xlsx' );
?>
|
Output:

Reference: https://phpspreadsheet.readthedocs.io/en/develop/topics/accessing-cells/
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
19 Nov, 2018
Like Article
Save Article