TIMEVALUE.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. error_reporting(E_ALL);
  3. set_time_limit(0);
  4. date_default_timezone_set('Europe/London');
  5. ?>
  6. <html>
  7. <head>
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  9. <title>PHPExcel Calculation Examples</title>
  10. </head>
  11. <body>
  12. <h1>TIMEVALUE</h1>
  13. <h2>Converts a time in the form of text to a serial number.</h2>
  14. <?php
  15. /** Include path **/
  16. set_include_path(get_include_path() . PATH_SEPARATOR . '../../../../Classes/');
  17. /** Include PHPExcel */
  18. include 'PHPExcel.php';
  19. // Create new PHPExcel object
  20. $objPHPExcel = new PHPExcel();
  21. $worksheet = $objPHPExcel->getActiveSheet();
  22. // Add some data
  23. $testDates = array( '3:15', '13:15', '15:15:15', '3:15 AM', '3:15 PM', '5PM', '9:15AM', '13:15AM'
  24. );
  25. $testDateCount = count($testDates);
  26. for($row = 1; $row <= $testDateCount; ++$row) {
  27. $worksheet->setCellValue('A'.$row, $testDates[$row-1]);
  28. $worksheet->setCellValue('B'.$row, '=TIMEVALUE(A'.$row.')');
  29. $worksheet->setCellValue('C'.$row, '=B'.$row);
  30. }
  31. $worksheet->getStyle('C1:C'.$testDateCount)
  32. ->getNumberFormat()
  33. ->setFormatCode('hh:mm:ss');
  34. echo '<hr />';
  35. // Test the formulae
  36. ?>
  37. <table border="1" cellspacing="0">
  38. <tr>
  39. <th>Time String</th>
  40. <th>Formula</th>
  41. <th>Excel TimeStamp</th>
  42. <th>Formatted TimeStamp</th>
  43. </tr>
  44. <?php
  45. for ($row = 1; $row <= $testDateCount; ++$row) {
  46. echo '<tr>';
  47. echo '<td>' , $worksheet->getCell('A'.$row)->getFormattedValue() , '</td>';
  48. echo '<td>' , $worksheet->getCell('B'.$row)->getValue() , '</td>';
  49. echo '<td>' , $worksheet->getCell('B'.$row)->getFormattedValue() , '</td>';
  50. echo '<td>' , $worksheet->getCell('C'.$row)->getFormattedValue() , '</td>';
  51. echo '</tr>';
  52. }
  53. ?>
  54. </table>