mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-23 18:18:21 +08:00
@@ -548,13 +548,17 @@ class Formatter extends Component
|
|||||||
$value = 0;
|
$value = 0;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
if (is_numeric($value)) {
|
if (is_numeric($value)) { // process as unix timestamp
|
||||||
// process as unix timestamp
|
|
||||||
if (($timestamp = DateTime::createFromFormat('U', $value)) === false) {
|
if (($timestamp = DateTime::createFromFormat('U', $value)) === false) {
|
||||||
throw new InvalidParamException("Failed to parse '$value' as a UNIX timestamp.");
|
throw new InvalidParamException("Failed to parse '$value' as a UNIX timestamp.");
|
||||||
}
|
}
|
||||||
return $timestamp;
|
return $timestamp;
|
||||||
|
} elseif (($timestamp = DateTime::createFromFormat('Y-m-d', $value)) !== false) { // try Y-m-d format
|
||||||
|
return $timestamp;
|
||||||
|
} elseif (($timestamp = DateTime::createFromFormat('Y-m-d H:i:s', $value)) !== false) { // try Y-m-d H:i:s format
|
||||||
|
return $timestamp;
|
||||||
}
|
}
|
||||||
|
// finally try to create a DateTime object with the value
|
||||||
$timestamp = new DateTime($value);
|
$timestamp = new DateTime($value);
|
||||||
return $timestamp;
|
return $timestamp;
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
|
|||||||
@@ -454,7 +454,8 @@ class FormatterTest extends TestCase
|
|||||||
public function dateInputs()
|
public function dateInputs()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
[false, '2014-13-01', 'yii\base\InvalidParamException'],
|
// ['2015-01-01 00:00:00', '2014-13-01'], // TODO evals to current time on that date
|
||||||
|
['2015-01-01 00:00:00', '2014-13-01 00:00:00'],
|
||||||
[false, 'asdfg', 'yii\base\InvalidParamException'],
|
[false, 'asdfg', 'yii\base\InvalidParamException'],
|
||||||
// [(string)strtotime('now'), 'now'], // fails randomly
|
// [(string)strtotime('now'), 'now'], // fails randomly
|
||||||
];
|
];
|
||||||
@@ -476,9 +477,9 @@ class FormatterTest extends TestCase
|
|||||||
if ($expectedException !== null) {
|
if ($expectedException !== null) {
|
||||||
$this->setExpectedException($expectedException);
|
$this->setExpectedException($expectedException);
|
||||||
}
|
}
|
||||||
$this->assertSame($expected, $this->formatter->asDate($value, 'php:U'));
|
$this->assertSame($expected, $this->formatter->asDate($value, 'yyyy-MM-dd HH:mm:ss'));
|
||||||
$this->assertSame($expected, $this->formatter->asTime($value, 'php:U'));
|
$this->assertSame($expected, $this->formatter->asTime($value, 'yyyy-MM-dd HH:mm:ss'));
|
||||||
$this->assertSame($expected, $this->formatter->asDatetime($value, 'php:U'));
|
$this->assertSame($expected, $this->formatter->asDatetime($value, 'yyyy-MM-dd HH:mm:ss'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user