Debian Codegraph @

vendor/ipl/web/src/Widget/TimeSince.php

raw
1 <?php
2
3 namespace ipl\Web\Widget;
4
5 use DateTime;
6 use Exception;
7 use ipl\I18n\Translation;
8
9 class TimeSince extends Time
10 {
11 protected $defaultAttributes = ['class' => 'time-since', 'data-relative-time' => 'since'];
12
13 /**
14 * @param int|float|DateTime|null $time Time as timestamp, DateTime object, or null for current time
15 * @param ?DateTime $compareTime Time to compare with, null for current time
16 *
17 * @throws Exception
18 */
19 public function __construct(int|float|DateTime|null $time = null, ?DateTime $compareTime = null)
20 {
21 $this->compareTime = $compareTime;
22
23 if (! $time instanceof DateTime) {
24 $time = $this->castToDateTime($time);
25 }
26
27 parent::__construct($time);
28 }
29
30 protected function format(): string
31 {
32 [$time, $type] = $this->diff($this->compareTime);
33
34 return sprintf(
35 match ($type) {
36 self::RELATIVE => $this->translate(
37 'for %s',
38 'A status is lasting for the given time interval'
39 ),
40 self::TIME, self::DATE => $this->translate(
41 'since %s',
42 'A status is lasting since the given time, date or date and time'
43 ),
44 },
45 $time
46 );
47 }
48 }