DATEVALUE.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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>DATEVALUE</h1>
  13. <h2>Converts a date 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( '26 March 2012', '29 Feb 2012', 'April 1, 2012', '25/12/2012',
  24. '2012-Oct-31', '5th November', 'January 1st', 'April 2012',
  25. '17-03', '03-2012', '29 Feb 2011', '03-05-07',
  26. '03-MAY-07', '03-13-07',
  27. );
  28. $testDateCount = count($testDates);
  29. for($row = 1; $row <= $testDateCount; ++$row) {
  30. $worksheet->setCellValue('A'.$row, $testDates[$row-1]);
  31. $worksheet->setCellValue('B'.$row, '=DATEVALUE(A'.$row.')');
  32. $worksheet->setCellValue('C'.$row, '=B'.$row);
  33. }
  34. $worksheet->getStyle('C1:C'.$testDateCount)
  35. ->getNumberFormat()
  36. ->setFormatCode('yyyy-mmm-dd');
  37. echo '<hr />';
  38. // Test the formulae
  39. ?>
  40. <p><strong>Warning: </strong>The PHPExcel DATEVALUE() function accepts a wider range of date formats than MS Excel's DATEFORMAT() function.</p>
  41. <table border="1" cellspacing="0">
  42. <tr>
  43. <th>Date String</th>
  44. <th>Formula</th>
  45. <th>Excel DateStamp</th>
  46. <th>Formatted DateStamp</th>
  47. </tr>
  48. <?php
  49. for ($row = 1; $row <= $testDateCount; ++$row) {
  50. echo '<tr>';
  51. echo '<td>' , $worksheet->getCell('A'.$row)->getFormattedValue() , '</td>';
  52. echo '<td>' , $worksheet->getCell('B'.$row)->getValue() , '</td>';
  53. echo '<td>' , $worksheet->getCell('B'.$row)->getFormattedValue() , '</td>';
  54. echo '<td>' , $worksheet->getCell('C'.$row)->getFormattedValue() , '</td>';
  55. echo '</tr>';
  56. }
  57. ?>
  58. </table>