TIMEVALUE.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  3. require __DIR__ . '/../../Header.php';
  4. $helper->log('Converts a time 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 = ['3:15', '13:15', '15:15:15', '3:15 AM', '3:15 PM', '5PM', '9:15AM', '13:15AM',
  10. ];
  11. $testDateCount = count($testDates);
  12. for ($row = 1; $row <= $testDateCount; ++$row) {
  13. $worksheet->setCellValue('A' . $row, $testDates[$row - 1]);
  14. $worksheet->setCellValue('B' . $row, '=TIMEVALUE(A' . $row . ')');
  15. $worksheet->setCellValue('C' . $row, '=B' . $row);
  16. }
  17. $worksheet->getStyle('C1:C' . $testDateCount)
  18. ->getNumberFormat()
  19. ->setFormatCode('hh:mm:ss');
  20. // Test the formulae
  21. for ($row = 1; $row <= $testDateCount; ++$row) {
  22. $helper->log('Time String: ' . $worksheet->getCell('A' . $row)->getFormattedValue());
  23. $helper->log('Formula: ' . $worksheet->getCell('B' . $row)->getValue());
  24. $helper->log('Excel TimeStamp: ' . $worksheet->getCell('B' . $row)->getFormattedValue());
  25. $helper->log('Formatted TimeStamp: ' . $worksheet->getCell('C' . $row)->getFormattedValue());
  26. $helper->log('');
  27. }