Commit b5cd0a6f by David Renty

asElapsedTime can now take a DateInterval string as a parameter

parent b7cf6183
...@@ -462,7 +462,7 @@ class Formatter extends Component ...@@ -462,7 +462,7 @@ class Formatter extends Component
* types of value are supported: * types of value are supported:
* *
* - an integer representing a UNIX timestamp * - an integer representing a UNIX timestamp
* - a string that can be parsed into a UNIX timestamp via `strtotime()` * - a string that can be parsed into a UNIX timestamp via `strtotime()` or that can be passed to a DateInterval constructor.
* - a PHP DateTime object * - a PHP DateTime object
* - a PHP DateInterval object * - a PHP DateInterval object
* *
...@@ -477,16 +477,27 @@ class Formatter extends Component ...@@ -477,16 +477,27 @@ class Formatter extends Component
if ($value instanceof \DateInterval) { if ($value instanceof \DateInterval) {
$interval = $value; $interval = $value;
} else { } else {
$value = $this->normalizeDatetimeValue($value); $timestamp = $this->normalizeDatetimeValue($value);
if ($timestamp === false) {
// $value is not a valid date/time value, so we try
// to create a DateInterval with it
try {
$interval = new \DateInterval($value);
} catch (Exception $e) {
// invalid date/time and invalid interval
return $this->nullDisplay;
}
} else {
$timezone = new \DateTimeZone($this->timeZone); $timezone = new \DateTimeZone($this->timeZone);
$dateNow = new DateTime('now', $timezone); $dateNow = new DateTime('now', $timezone);
$dateThen = new DateTime(null, $timezone); $dateThen = new DateTime(null, $timezone);
$dateThen->setTimestamp($value); $dateThen->setTimestamp($timestamp);
$interval = $dateNow->diff($dateThen); $interval = $dateNow->diff($dateThen);
} }
}
if ($interval->y >= 1) { if ($interval->y >= 1) {
$delta = $interval->y; $delta = $interval->y;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment