diff --git a/php-export-data.class.php b/php-export-data.class.php index 440e1c4..cad7434 100755 --- a/php-export-data.class.php +++ b/php-export-data.class.php @@ -198,7 +198,7 @@ function generateRow($row) { return $output; } - private function generateCell($item) { + protected function generateCell($item) { $output = ''; $style = ''; @@ -211,7 +211,14 @@ private function generateCell($item) { // Note we want to be very strict in what we consider a date. There is the possibility // of really screwing up the data if we try to reformat a string that was not actually // intended to represent a date. - elseif(preg_match("/^(\d{1,2}|\d{4})[\/\-]\d{1,2}[\/\-](\d{1,2}|\d{4})([^\d].+)?$/",$item) && + elseif (preg_match('_^ + # dates: 2010-07-14 or 7/14/2010 + (?:\d{1,2}|\d{4})[/-]\d{1,2}[/-](?:\d{1,2}|\d{4}) + # optional time + (?:[T\s]+ # separated by space or T + [\d:,]+ # hours:minutes + )? + $_x', $item) && ($timestamp = strtotime($item)) && ($timestamp > 0) && ($timestamp < strtotime('+500 years'))) { @@ -237,4 +244,4 @@ function sendHttpHeaders() { header("Content-Disposition: inline; filename=\"" . basename($this->filename) . "\""); } -} \ No newline at end of file +} diff --git a/test/ExportDataExcelTest.php b/test/ExportDataExcelTest.php new file mode 100644 index 0000000..ed3ede6 --- /dev/null +++ b/test/ExportDataExcelTest.php @@ -0,0 +1,54 @@ +generateCell($item); + } +} + +/** + * + */ +class ExportDataExcelTest extends PHPUnit_Framework_TestCase { + protected $output = 'browser'; + /** + * @var ExportDataExcel + */ + protected $object; + + /** + * Sets up the fixture + */ + protected function setUp() { + $this->object = new ExportDataExcelWrap($this->output); + } + + /** + * @covers ExportDataExcel::generateCell + * @dataProvider dateProvider + */ + public function testDates($exp, $date) { + $res = $this->object->wrapGenerateCell($date); + $this->assertNotEmpty($res); + $res = trim($res); + $this->assertEquals($exp, $res); + } + + public function dateProvider() { + return array( + // iso dates: https://en.wikipedia.org/wiki/ISO_8601 + array('2010-01-02T00:00:00',"2010-01-02"), + array('2010-01-02T09:52:00', "2010-01-02 9:52"), + array('2010-01-02T10:00:00', "2010-01-02T10:00"), + array('2010-01-02T10:00:00', "2010-01-02T10:00:00"), + # microseconds not supported by strtotime +# array('2010-01-02T06:12:00', "2010-01-02T06:12:00,000"), +# array('2010-01-02T00:00:00', "2010-01-02T10:00:00,000"), + // US dates + array('2010-02-08T00:00:00', "02/08/2010 00:00"), + array('2010-02-08T00:00:00', "02/08/2010"), + array('2010-02-09T00:00:00', "2/9/2010"), + ); + } +}