Add $requireTime to timeAgo() and timeSince() too

This commit is contained in:
Eric Lippmann 2019-07-11 14:28:50 +02:00
parent 037bcc4b3e
commit f9021e8fdd
2 changed files with 10 additions and 8 deletions

View File

@ -149,12 +149,13 @@ class DateFormatter
* *
* @param int|float $time * @param int|float $time
* @param bool $timeOnly * @param bool $timeOnly
* @param bool $requireTime
* *
* @return string * @return string
*/ */
public static function timeAgo($time, $timeOnly = false) public static function timeAgo($time, $timeOnly = false, $requireTime = false)
{ {
list($type, $ago, $invert) = static::diff($time); list($type, $ago, $invert) = static::diff($time, $requireTime);
if ($timeOnly) { if ($timeOnly) {
return $ago; return $ago;
} }
@ -185,12 +186,13 @@ class DateFormatter
* *
* @param int|float $time * @param int|float $time
* @param bool $timeOnly * @param bool $timeOnly
* @param bool $requireTime
* *
* @return string * @return string
*/ */
public static function timeSince($time, $timeOnly = false) public static function timeSince($time, $timeOnly = false, $requireTime = false)
{ {
list($type, $since, $invert) = static::diff($time); list($type, $since, $invert) = static::diff($time, $requireTime);
if ($timeOnly) { if ($timeOnly) {
return $since; return $since;
} }

View File

@ -38,25 +38,25 @@ $this->addHelperFunction('formatTime', function ($time) {
return DateFormatter::formatTime($time); return DateFormatter::formatTime($time);
}); });
$this->addHelperFunction('timeAgo', function ($time, $timeOnly = false) { $this->addHelperFunction('timeAgo', function ($time, $timeOnly = false, $requireTime = false) {
if (! $time) { if (! $time) {
return ''; return '';
} }
return sprintf( return sprintf(
'<span class="relative-time time-ago" title="%s">%s</span>', '<span class="relative-time time-ago" title="%s">%s</span>',
DateFormatter::formatDateTime($time), DateFormatter::formatDateTime($time),
DateFormatter::timeAgo($time, $timeOnly) DateFormatter::timeAgo($time, $timeOnly, $requireTime)
); );
}); });
$this->addHelperFunction('timeSince', function ($time, $timeOnly = false) { $this->addHelperFunction('timeSince', function ($time, $timeOnly = false, $requireTime = false) {
if (! $time) { if (! $time) {
return ''; return '';
} }
return sprintf( return sprintf(
'<span class="relative-time time-since" title="%s">%s</span>', '<span class="relative-time time-since" title="%s">%s</span>',
DateFormatter::formatDateTime($time), DateFormatter::formatDateTime($time),
DateFormatter::timeSince($time, $timeOnly) DateFormatter::timeSince($time, $timeOnly, $requireTime)
); );
}); });