Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
durationmanager.php
1<?php
2
4
6
8{
9 private const MINUTE_LENGTH = 60;
10 private const HOUR_LENGTH = 60 * self::MINUTE_LENGTH;
11 private const DAY_LENGTH = 24 * self::HOUR_LENGTH;
12
13 private Date $start;
14 private Date $end;
15
16 public function __construct(Date $start, Date $end)
17 {
18 $this->start = $start;
19 $this->end = $end;
20 }
21
26 public function getFormattedDuration(): string
27 {
28 $duration = $this->getDuration();
29 if (!$this->isPositiveDuration())
30 {
31 $result = FormatDate('idiff', time(), time());
32 }
33
34 if (empty($result))
35 {
36 $result = '';
37 if ($duration >= self::DAY_LENGTH)
38 {
39 $result .= FormatDate('ddiff', time(), $duration + time()) . " ";
40 }
41 $duration = $duration % self::DAY_LENGTH;
42 if ($duration >= self::HOUR_LENGTH)
43 {
44 $result .= FormatDate('Hdiff', time(), $duration + time()) . " ";
45 }
46 $duration = $duration % self::HOUR_LENGTH;
47 if ($duration >= self::MINUTE_LENGTH)
48 {
49 $result .= FormatDate('idiff', time(), $duration + time());
50 }
51 }
52
53 return trim($result);
54 }
55
60 public function isPositiveDuration(): bool
61 {
62 return $this->getDuration() >= self::MINUTE_LENGTH;
63 }
64
69 public function getDuration(): int
70 {
71 return $this->calculateDuration($this->start->getTimestamp(), $this->end->getTimestamp());
72 }
73
79 private function calculateDuration(int $start, int $end): int
80 {
81 return $end - $start;
82 }
83
89 public function areDurationsEqual(Date $start, Date $end): bool
90 {
91 return $this->getDuration() === $this->calculateDuration($start->getTimestamp(), $end->getTimestamp());
92 }
93}