DATEVALUE.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  3. require __DIR__ . '/../../Header.php';
  4. $helper->log('Converts a date in the form of text to a serial number.');
  5. // Create new PhpSpreadsheet object
  6. $spreadsheet = new Spreadsheet();
  7. $worksheet = $spreadsheet->getActiveSheet();
  8. // Add some data
  9. $testDates = ['26 March 2012', '29 Feb 2012', 'April 1, 2012', '25/12/2012',
  10. '2012-Oct-31', '5th November', 'January 1st', 'April 2012',
  11. '17-03', '03-2012', '29 Feb 2011', '03-05-07',
  12. '03-MAY-07', '03-13-07',
  13. ];
  14. $testDateCount = count($testDates);
  15. for ($row = 1; $row <= $testDateCount; ++$row) {
  16. $worksheet->setCellValue('A' . $row, $testDates[$row - 1]);
  17. $worksheet->setCellValue('B' . $row, '=DATEVALUE(A' . $row . ')');
  18. $worksheet->setCellValue('C' . $row, '=B' . $row);
  19. }
  20. $worksheet->getStyle('C1:C' . $testDateCount)
  21. ->getNumberFormat()
  22. ->setFormatCode('yyyy-mmm-dd');
  23. // Test the formulae
  24. $helper->log('<strong>Warning: </strong>The PhpSpreadsheet DATEVALUE() function accepts a wider range of date formats than MS Excel DATEFORMAT() function.');
  25. for ($row = 1; $row <= $testDateCount; ++$row) {
  26. $helper->log('Date String: ' . $worksheet->getCell('A' . $row)->getFormattedValue());
  27. $helper->log('Formula: ' . $worksheet->getCell('B' . $row)->getValue());
  28. $helper->log('Excel DateStamp: ' . $worksheet->getCell('B' . $row)->getFormattedValue());
  29. $helper->log('Formatted DateStamp' . $worksheet->getCell('C' . $row)->getFormattedValue());
  30. $helper->log('');
  31. }