Files
PhpSpreadsheet/samples/DateTime/DATE.php
T
oleibman b3de003aa5 Reorganize Samples
The samples have become unwieldy when running them from a browser. In particular, the drop-down lists are fixed size with no scrolling, and many of them are now just too large. I have moved all the Calculation samples up a level, and broken several categories (Basic, Chart, DateTime, Engineering, Financial, and Reader) into several pieces.

Convert-Online (now found in the Engineering category) had a number of different problems which are now resolved. It is the only member with any significant code change.
2024-02-03 07:25:29 -08:00

46 lines
1.7 KiB
PHP

<?php
use PhpOffice\PhpSpreadsheet\Spreadsheet;
require __DIR__ . '/../../Header.php';
$category = 'Date/Time';
$functionName = 'DATE';
$description = 'Returns the Excel serial number of a particular date';
$helper->titles($category, $functionName, $description);
// Create new PhpSpreadsheet object
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();
// Add some data
$testDates = [[2012, 3, 26], [2012, 2, 29], [2012, 4, 1], [2012, 12, 25],
[2012, 10, 31], [2012, 11, 5], [2012, 1, 1], [2012, 3, 17],
[2011, 2, 29], [7, 5, 3], [2012, 13, 1], [2012, 11, 45],
[2012, 0, 0], [2012, 1, 0], [2012, 0, 1],
[2012, -2, 2], [2012, 2, -2], [2012, -2, -2],
];
$testDateCount = count($testDates);
$worksheet->fromArray($testDates, null, 'A1', true);
for ($row = 1; $row <= $testDateCount; ++$row) {
$worksheet->setCellValue('D' . $row, '=DATE(A' . $row . ',B' . $row . ',C' . $row . ')');
$worksheet->setCellValue('E' . $row, '=D' . $row);
}
$worksheet->getStyle('E1:E' . $testDateCount)
->getNumberFormat()
->setFormatCode('yyyy-mm-dd');
// Test the formulae
for ($row = 1; $row <= $testDateCount; ++$row) {
$helper->log("(A{$row}) Year: " . $worksheet->getCell('A' . $row)->getFormattedValue());
$helper->log("(B{$row}) Month: " . $worksheet->getCell('B' . $row)->getFormattedValue());
$helper->log("(C{$row}) Day: " . $worksheet->getCell('C' . $row)->getFormattedValue());
$helper->log('Formula: ' . $worksheet->getCell('D' . $row)->getValue());
$helper->log('Excel DateStamp: ' . $worksheet->getCell('D' . $row)->getCalculatedValue());
$helper->log('Formatted DateStamp: ' . $worksheet->getCell('E' . $row)->getFormattedValue());
$helper->log('');
}