summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/web/src/Widget/TimeAgo.php
blob: cbd0dad24833c26a7f5caab4c98f695bce8272d3 (plain)
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
<?php

namespace ipl\Web\Widget;

use Icinga\Date\DateFormatter;
use ipl\Html\BaseHtmlElement;

class TimeAgo extends BaseHtmlElement
{
    /** @var int */
    protected $ago;

    protected $tag = 'time';

    protected $defaultAttributes = ['class' => 'time-ago'];

    public function __construct($ago)
    {
        $this->ago = (int) $ago;
    }

    protected function assemble()
    {
        $dateTime = DateFormatter::formatDateTime($this->ago);

        $this->addAttributes([
            'datetime' => $dateTime,
            'title'    => $dateTime
        ]);

        $this->add(DateFormatter::timeAgo($this->ago));
    }
}