DateTime   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 76
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 4 1
A isSameMonthAndYear() 0 4 1
A isInPreviousMonth() 0 4 1
A startOfMonth() 0 6 1
A endOfMonth() 0 6 1
A getMonthsBetween() 0 17 1
1
<?php
2
3
namespace JhFlexiTime\DateTime;
4
5
use DatePeriod;
6
7
/**
8
 * Class DateTime
9
 * @package JhFlexiTime\DateTime
10
 * @author Aydin Hassan <[email protected]>
11
 */
12
class DateTime extends \DateTime
13
{
14
    /**
15
     * @return string
16
     */
17
    public function __toString()
18
    {
19
        return $this->format('U');
20
    }
21
22
    /**
23
     * Check if given date is in same month and year
24
     *
25
     * @param DateTime $date
26
     * @return bool
27
     */
28
    public function isSameMonthAndYear(DateTime $date)
29
    {
30
        return $this->format('m-Y') === $date->format('m-Y');
31
    }
32
33
    /**
34
     * @param DateTime $date
35
     * @return bool
36
     */
37
    public function isInPreviousMonth(DateTime $date)
38
    {
39
        return $date < $this->startOfMonth();
40
    }
41
42
    /**
43
     * Immutable function to get the start of this month
44
     *
45
     * @return DateTime
46
     */
47
    public function startOfMonth()
48
    {
49
        $date = clone $this;
50
        $date->modify('first day of this month 00:00:00');
51
        return $date;
52
    }
53
54
    /**
55
     * Immutable function to get the end of this month
56
     *
57
     * @return DateTime
58
     */
59
    public function endOfMonth()
60
    {
61
        $date = clone $this;
62
        $date->modify('last day of this month 23:59:59');
63
        return $date;
64
    }
65
66
    /**
67
     * @param DateTime $date
68
     * @return DateTime[]
69
     */
70
    public function getMonthsBetween(DateTime $date)
71
    {
72
        return array_map(
73
            function (\DateTime $date) {
74
                $jhDate = new DateTime;
75
                $jhDate->setTimestamp($date->getTimestamp());
76
                return $jhDate;
77
            },
78
            iterator_to_array(
79
                new DatePeriod(
80
                    $this->startOfMonth(),
81
                    new \DateInterval('P1M'),
82
                    $date
83
                )
84
            )
85
        );
86
    }
87
}
88