DateHelper   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 33
eloc 80
dl 0
loc 127
rs 9.76
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
D buildDateTimeRangeFromTwoInputs() 0 79 24
B diffToText() 0 24 7
A buildDateTimeSince() 0 11 2
1
<?php
2
3
namespace Dpeuscher\Util\Date;
4
5
use DateInterval;
6
7
/**
8
 * @category  util
9
 * @copyright Copyright (c) 2018 Dominik Peuscher
10
 */
11
class DateHelper
12
{
13
    /**
14
     * @param $fromMonth
15
     * @param $toMonth
16
     * @param string $defaultInterval
17
     * @return \DateTime[]
18
     * @throws \Exception
19
     */
20
    public function buildDateTimeRangeFromTwoInputs($fromMonth, $toMonth = null, $defaultInterval = 'P1M'): array
21
    {
22
        if (preg_match('/^([0-9]{1,2})\.([0-9]{1,2})\.?$/', $fromMonth, $matches)) {
23
            $fromDateTime = new \DateTime(date('Y-') . $matches[2] . '-' . $matches[1]);
24
            if (!isset($toMonth)) {
25
                $toDateTime = clone $fromDateTime;
26
                $toDateTime->add(new DateInterval($defaultInterval));
27
            }
28
            if (isset($toDateTime)) {
29
                return [$fromDateTime, $toDateTime];
30
            }
31
            if (preg_match('/^([0-9]{1,2})\.([0-9]{1,2})\.?$/', $toMonth, $matches2)) {
32
                return [
33
                    $fromDateTime,
34
                    new \DateTime(($matches2[2] < $matches[2] ? 1 + date('Y') : date('Y')) . '-' . $matches2[2] . '-' . $matches2[1]),
35
                ];
36
            }
37
            return [$fromDateTime, new \DateTime($toMonth)];
38
        }
39
        if (preg_match('/^([0-9]{1,2})-[0-9]{1,2}$/', $fromMonth, $matches)) {
40
            $fromDateTime = new \DateTime(date('Y-') . $fromMonth);
41
            if (!isset($toMonth)) {
42
                $toDateTime = clone $fromDateTime;
43
                $toDateTime->add(new DateInterval($defaultInterval));
44
            }
45
            if (isset($toDateTime)) {
46
                return [$fromDateTime, $toDateTime];
47
            }
48
            if (preg_match('/^([0-9]{1,2})-[0-9]{1,2}$/', $toMonth, $matches2)) {
49
                return [
50
                    $fromDateTime,
51
                    new \DateTime(($matches2[1] < $matches[1] ? 1 + date('Y') : date('Y')) . '-' . $toMonth),
52
                ];
53
            }
54
            if (preg_match('/^([0-9]{1,2})\.([0-9]{1,2})\.?$/', $toMonth, $matches2)) {
55
                return [
56
                    $fromDateTime,
57
                    new \DateTime(($matches2[2] < $matches[1] ? 1 + date('Y') : date('Y')) . '-' . $matches2[2] . '-' . $matches2[1]),
58
                ];
59
            }
60
            return [$fromDateTime, new \DateTime($toMonth)];
61
        }
62
        if (preg_match('/^([0-9]{1,2})$/', $fromMonth, $matches)) {
63
            if (!isset($toMonth)) {
64
                $toMonth = (string)($fromMonth >= 12 ? 1 : $fromMonth + 1);
65
            }
66
            $fromYear = date('Y');
67
            $fromDateTime = new \DateTime($fromYear . '-' . str_pad($fromMonth, 2, 0, STR_PAD_LEFT) . '-01');
68
            if (preg_match('/^[0-9]{1,2}$/', $toMonth)) {
69
                $toYear = $fromYear;
70
                if ((int)$toMonth < (int)$fromMonth) {
71
                    $toYear += 1;
72
                }
73
                $toDateTime = new \DateTime($toYear . '-' . str_pad($toMonth, 2, 0, STR_PAD_LEFT) . '-01');
74
                return [$fromDateTime, $toDateTime];
75
            }
76
            if (preg_match('/^([0-9]{1,2})-[0-9]{1,2}$/', $toMonth, $matches2)) {
77
                return [
78
                    $fromDateTime,
79
                    new \DateTime(($matches2[1] < $matches[1] ? 1 + date('Y') : date('Y')) . '-' . $toMonth),
80
                ];
81
            }
82
            if (preg_match('/^([0-9]{1,2})\.([0-9]{1,2})\.?$/', $toMonth, $matches2)) {
83
                return [
84
                    $fromDateTime,
85
                    new \DateTime(($matches2[2] < $matches[1] ? 1 + date('Y') : date('Y')) . '-' . $matches2[2] . '-' . $matches2[1]),
86
                ];
87
            }
88
            return [$fromDateTime, new \DateTime($toMonth)];
89
        }
90
        $fromDateTime = new \DateTime($fromMonth);
91
        if (!isset($toMonth)) {
92
            $toDateTime = clone $fromDateTime;
93
            $toDateTime->add(new DateInterval($defaultInterval));
94
        }
95
        if (isset($toDateTime)) {
96
            return [$fromDateTime, $toDateTime];
97
        }
98
        return [$fromDateTime, new \DateTime($toMonth)];
99
    }
100
101
    public function buildDateTimeSince(string $sinceString = 'P7D'): \DateTime
102
    {
103
        try {
104
            $dateInterval = new DateInterval($sinceString);
105
        } catch (\Exception $e) {
106
            /** @noinspection PhpUnhandledExceptionInspection */
107
            $dateInterval = new DateInterval('P1D');
108
        }
109
        $dateTime = new \DateTime();
110
        $dateTime->sub($dateInterval);
111
        return $dateTime;
112
    }
113
114
    public function diffToText(DateInterval $diff): string
115
    {
116
        $text = [];
117
        if ($diff->days) {
118
            $text[] = $diff->days . 'd';
119
        } elseif ($diff->days === false) {
120
            $now = new \DateTime();
121
            $then = clone $now;
122
            $now->add($diff);
123
            $interval = $now->diff($then);
124
            if ($interval->days) {
125
                $text[] = $interval->days . 'd';
126
            }
127
        }
128
        if ($diff->h) {
129
            $text[] = $diff->h . 'h';
130
        }
131
        if ($diff->i) {
132
            $text[] = $diff->i . 'm';
133
        }
134
        if ($diff->s) {
135
            $text[] = $diff->s . 's';
136
        }
137
        return implode(' ', $text);
138
    }
139
}
140