DATE.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  3. require __DIR__ . '/../../Header.php';
  4. $helper->log('Returns the serial number of a particular date.');
  5. // Create new PhpSpreadsheet object
  6. $spreadsheet = new Spreadsheet();
  7. $worksheet = $spreadsheet->getActiveSheet();
  8. // Add some data
  9. $testDates = [[2012, 3, 26], [2012, 2, 29], [2012, 4, 1], [2012, 12, 25],
  10. [2012, 10, 31], [2012, 11, 5], [2012, 1, 1], [2012, 3, 17],
  11. [2011, 2, 29], [7, 5, 3], [2012, 13, 1], [2012, 11, 45],
  12. [2012, 0, 0], [2012, 1, 0], [2012, 0, 1],
  13. [2012, -2, 2], [2012, 2, -2], [2012, -2, -2],
  14. ];
  15. $testDateCount = count($testDates);
  16. $worksheet->fromArray($testDates, null, 'A1', true);
  17. for ($row = 1; $row <= $testDateCount; ++$row) {
  18. $worksheet->setCellValue('D' . $row, '=DATE(A' . $row . ',B' . $row . ',C' . $row . ')');
  19. $worksheet->setCellValue('E' . $row, '=D' . $row);
  20. }
  21. $worksheet->getStyle('E1:E' . $testDateCount)
  22. ->getNumberFormat()
  23. ->setFormatCode('yyyy-mmm-dd');
  24. // Test the formulae
  25. for ($row = 1; $row <= $testDateCount; ++$row) {
  26. $helper->log('Year: ' . $worksheet->getCell('A' . $row)->getFormattedValue());
  27. $helper->log('Month: ' . $worksheet->getCell('B' . $row)->getFormattedValue());
  28. $helper->log('Day: ' . $worksheet->getCell('C' . $row)->getFormattedValue());
  29. $helper->log('Formula: ' . $worksheet->getCell('D' . $row)->getValue());
  30. $helper->log('Excel DateStamp: ' . $worksheet->getCell('D' . $row)->getFormattedValue());
  31. $helper->log('Formatted DateStamp: ' . $worksheet->getCell('E' . $row)->getFormattedValue());
  32. $helper->log('');
  33. }