/** * @dataProvider dateTimeFilterWithExceptionDataProvider */ public function testFilterWithException($inputData) { $this->setExpectedException('\\Exception'); $localeMock = $this->getMock('\\Magento\\Framework\\Stdlib\\DateTime\\TimezoneInterface'); $localeMock->expects($this->once())->method('getDateFormat')->with(\IntlDateFormatter::SHORT)->will($this->returnValue('MM-dd-yyyy')); $model = new DateTime($localeMock); $model->filter($inputData); }
public function testFilter() { $localeMock = $this->getMock('\\Magento\\Framework\\Stdlib\\DateTime\\TimezoneInterface'); $localeMock->expects($this->once())->method('getDateTimeFormat')->with(\IntlDateFormatter::SHORT)->will($this->returnValue('HH:mm:ss MM-dd-yyyy')); $model = new DateTime($localeMock); // Check that datetime is converted to 'yyyy-MM-dd HH:mm:ss' format $this->assertEquals('2241-12-31 23:59:53', $model->filter('23:59:53 12-31-2241')); }
/** * Convert date from localized to internal format * * @param string $value * @return string * @throws LocalizedException */ public function filter($value) { try { $value = new \DateTime($value); return $value->format('Y-m-d'); } catch (\Exception $e) { throw new \Exception('Invalid input date format'); } }
/** * Convert date from localized to internal format * * @param string $value * @return string * @throws \Exception */ public function filter($value) { try { $dateTime = new \DateTime($value); return $dateTime->format('Y-m-d H:i:s'); } catch (\Exception $e) { throw new \Exception("Invalid input datetime format of value '{$value}'", $e->getCode(), $e); } }