DATE.php 1.7 KB

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