TIME.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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>TIME</h1>
  13. <h2>Returns the serial number of a particular time.</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( array(3,15), array(13,15), array(15,15,15), array(3,15,30),
  24. array(15,15,15), array(5), array(9,15,0), array(9,15,-1),
  25. array(13,-14,-15), array(0,0,-1)
  26. );
  27. $testDateCount = count($testDates);
  28. $worksheet->fromArray($testDates,NULL,'A1',true);
  29. for ($row = 1; $row <= $testDateCount; ++$row) {
  30. $worksheet->setCellValue('D'.$row, '=TIME(A'.$row.',B'.$row.',C'.$row.')');
  31. $worksheet->setCellValue('E'.$row, '=D'.$row);
  32. }
  33. $worksheet->getStyle('E1:E'.$testDateCount)
  34. ->getNumberFormat()
  35. ->setFormatCode('hh:mm:ss');
  36. echo '<hr />';
  37. // Test the formulae
  38. ?>
  39. <table border="1" cellspacing="0">
  40. <tr>
  41. <th colspan="3">Date Value</th>
  42. <th rowspan="2" valign="bottom">Formula</th>
  43. <th rowspan="2" valign="bottom">Excel TimeStamp</th>
  44. <th rowspan="2" valign="bottom">Formatted TimeStamp</th>
  45. </tr>
  46. <tr>
  47. <th>Hour</th>
  48. <th>Minute</th>
  49. <th>Second</th>
  50. <tr>
  51. <?php
  52. for ($row = 1; $row <= $testDateCount; ++$row) {
  53. echo '<tr>';
  54. echo '<td>' , $worksheet->getCell('A'.$row)->getFormattedValue() , '</td>';
  55. echo '<td>' , $worksheet->getCell('B'.$row)->getFormattedValue() , '</td>';
  56. echo '<td>' , $worksheet->getCell('C'.$row)->getFormattedValue() , '</td>';
  57. echo '<td>' , $worksheet->getCell('D'.$row)->getValue() , '</td>';
  58. echo '<td>' , $worksheet->getCell('D'.$row)->getFormattedValue() , '</td>';
  59. echo '<td>' , $worksheet->getCell('E'.$row)->getFormattedValue() , '</td>';
  60. echo '</tr>';
  61. }
  62. ?>
  63. </table>