-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTimeAgo.php
More file actions
41 lines (33 loc) · 1.09 KB
/
TimeAgo.php
File metadata and controls
41 lines (33 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
namespace ipl\Web\Widget;
use ipl\Html\Attributes;
use ipl\Html\Text;
class TimeAgo extends Time
{
protected $defaultAttributes = ['class' => 'time-ago'];
protected function assemble(): void
{
$this->addAttributes(
Attributes::create(
[
'datetime' => $this->timeString,
'data-relative-time' => 'ago'
]
)
);
$this->addHtml(Text::create($this->format()));
}
protected function format(): string
{
static $onMessage = ['on %s', 'An event happened on the given date or date and time'];
static $map = [
self::RELATIVE => ['%s ago', 'An event that happened the given time interval ago'],
self::TIME => ['at %s', 'An event happened at the given time'],
self::DATE => null,
self::DATETIME => null,
];
[$time, $type] = $this->diff($this->dateTime);
$format = $map[$type] ?? $onMessage;
return sprintf(t(N_($format[0]), N_($format[1])), $time);
}
}