0
0

DATE.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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>DATE</h1>
  13. <h2>Returns the serial number of a particular date.</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(2012,3,26), array(2012,2,29), array(2012,4,1), array(2012,12,25),
  24. array(2012,10,31), array(2012,11,5), array(2012,1,1), array(2012,3,17),
  25. array(2011,2,29), array(7,5,3), array(2012,13,1), array(2012,11,45),
  26. array(2012,0,0), array(2012,1,0), array(2012,0,1),
  27. array(2012,-2,2), array(2012,2,-2), array(2012,-2,-2),
  28. );
  29. $testDateCount = count($testDates);
  30. $worksheet->fromArray($testDates,NULL,'A1',true);
  31. for ($row = 1; $row <= $testDateCount; ++$row) {
  32. $worksheet->setCellValue('D'.$row, '=DATE(A'.$row.',B'.$row.',C'.$row.')');
  33. $worksheet->setCellValue('E'.$row, '=D'.$row);
  34. }
  35. $worksheet->getStyle('E1:E'.$testDateCount)
  36. ->getNumberFormat()
  37. ->setFormatCode('yyyy-mmm-dd');
  38. echo '<hr />';
  39. // Test the formulae
  40. ?>
  41. <table border="1" cellspacing="0">
  42. <tr>
  43. <th colspan="3">Date Value</th>
  44. <th rowspan="2" valign="bottom">Formula</th>
  45. <th rowspan="2" valign="bottom">Excel DateStamp</th>
  46. <th rowspan="2" valign="bottom">Formatted DateStamp</th>
  47. </tr>
  48. <tr>
  49. <th>Year</th>
  50. <th>Month</th>
  51. <th>Day</th>
  52. <tr>
  53. <?php
  54. for ($row = 1; $row <= $testDateCount; ++$row) {
  55. echo '<tr>';
  56. echo '<td>' , $worksheet->getCell('A'.$row)->getFormattedValue() , '</td>';
  57. echo '<td>' , $worksheet->getCell('B'.$row)->getFormattedValue() , '</td>';
  58. echo '<td>' , $worksheet->getCell('C'.$row)->getFormattedValue() , '</td>';
  59. echo '<td>' , $worksheet->getCell('D'.$row)->getValue() , '</td>';
  60. echo '<td>' , $worksheet->getCell('D'.$row)->getFormattedValue() , '</td>';
  61. echo '<td>' , $worksheet->getCell('E'.$row)->getFormattedValue() , '</td>';
  62. echo '</tr>';
  63. }
  64. ?>
  65. </table>