-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMonth.php
More file actions
116 lines (102 loc) · 2.69 KB
/
Month.php
File metadata and controls
116 lines (102 loc) · 2.69 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
namespace Icecave\Chrono\Interval;
use Icecave\Chrono\Date;
use Icecave\Chrono\Detail\Calendar;
use Icecave\Chrono\Detail\Iso8601;
use Icecave\Chrono\Iso8601Interface;
use Icecave\Chrono\TimePointInterface;
class Month extends AbstractInterval implements Iso8601Interface
{
/**
* @param Year $year The year.
* @param int $ordinal The month number.
*/
public function __construct(Year $year, $ordinal)
{
$this->year = $year;
$this->ordinal = $ordinal;
}
/**
* @param TimePointInterface $timePoint The time point to create from.
*
* @return Month The Month constructed from the given time point.
*/
public static function fromTimePoint(TimePointInterface $timePoint)
{
return new self(
new Year($timePoint->year()),
$timePoint->month()
);
}
/**
* Standard year month format:
* YYYY-MM
*
* @link http://en.wikipedia.org/wiki/ISO_8601#Calendar_dates
*
* @param string $isoString A string containing a year month in any ISO-8601 compatible year month format.
*
* @return Month The Month constructed from the ISO compatible string.
*/
public static function fromIsoString($isoString)
{
$result = Iso8601::parseYearMonth($isoString);
return new self(
new Year($result['year']),
$result['month']
);
}
/**
* @return Year
*/
public function year()
{
return $this->year;
}
/**
* @return int The year number.
*/
public function ordinal()
{
return $this->ordinal;
}
/**
* @return Date The start of the interval.
*/
public function start()
{
return new Date($this->year()->ordinal(), $this->ordinal(), 1);
}
/**
* @return Date The end of the interval.
*/
public function end()
{
return new Date($this->year()->ordinal(), $this->ordinal() + 1, 1);
}
/**
* @return int The number of days included in this interval.
*/
public function numberOfDays()
{
return Calendar::daysInMonth($this->year()->ordinal(), $this->ordinal());
}
/**
* @return string A string representing this object in an ISO compatible year month format (YYYY-MM).
*/
public function isoString()
{
return Iso8601::formatYearMonth(
$this->year()->ordinal(),
$this->ordinal()
);
}
/**
* @return string A string representing this object in an ISO compatible year month format (YYYY-MM).
*/
public function __toString()
{
return $this->isoString();
}
private $ordinal;
}