-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimePeriod.php
More file actions
140 lines (125 loc) · 3.63 KB
/
TimePeriod.php
File metadata and controls
140 lines (125 loc) · 3.63 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/**
* Arikaim
*
* @link http://www.arikaim.com
* @copyright Copyright (c) Konstantin Atanasov <info@arikaim.com>
* @license http://www.arikaim.com/license
*
*/
namespace Arikaim\Core\Utils;
use Arikaim\Core\Utils\DateTime;
/**
* Time periods
*/
class TimePeriod
{
/**
* Check if timestamp is today
*
* @param int $timestamp
* @return bool
*/
public static function isToday(int $timestamp): bool
{
$period = Self::getDayPeriod();
return ($timestamp >= $period['start'] && $timestamp >= $period['end']);
}
/**
* Check if timestamp is in year
*
* @param int $timestamp
* @param int|null $year
* @return bool
*/
public static function isYear(int $timestamp, ?int $year = null): bool
{
$period = Self::getYearPeriod($year);
return ($timestamp >= $period['start'] && $timestamp >= $period['end']);
}
/**
* Check if timestamp is in month
*
* @param int $timestamp
* @param int|null $month
* @param int|null $year
* @return bool
*/
public static function isMonth(int $timestamp, ?int $month = null, ?int $year = null): bool
{
$period = Self::getMonthPeriod($month,$year);
return ($timestamp >= $period['start'] && $timestamp >= $period['end']);
}
/**
* Get month period
*
* @param int|null $year
* @return array
*/
public static function getYearPeriod(?int $year = null): array
{
$year = $year ?? \date('Y');
return Self::getPeriod(
$year . '-01-01T00:00:00Z',
$year . '-12-31T23:59:59Z'
);
}
/**
* Get month period
*
* @param int|null $month
* @param int|null $year
* @return array
*/
public static function getMonthPeriod(?int $month = null, ?int $year = null): array
{
$year = (empty($year) == true) ? \date('Y') : $year;
$month = (empty($month) == true) ? \date('m') : $month;
$lastDay = DateTime::getLastDay($month);
return Self::getPeriod(
$year . '-' . $month . '-01T00:00:00Z',
$year . '-' . $month . '-' . $lastDay . 'T23:59:59Z'
);
}
/**
* Get day period
*
* @param int|null $day
* @param int|null $month
* @param int|null $year
* @return array
*/
public static function getDayPeriod(?int $day = null, ?int $month = null, ?int $year = null): array
{
$day = (empty($day) == true) ? \date('j') : $day;
$year = (empty($year) == true) ? \date('Y') : $year;
$month = (empty($month) == true) ? \date('m') : $month;
return Self::getPeriod(
$year . '-' . $month . '-' . $day . 'T00:00:00Z',
$year . '-' . $month . '-' . $day . 'T23:59:59Z'
);
}
/**
* Get yesterday time period
*
* @return array
*/
public static function getYesterdayPeriod(): array
{
return Self::getDayPeriod(\date('m'),\date('j') - 1,\date('Y'));
}
/**
* Get period
*
* @param string $fromDate
* @param string $toDate
* @return array
*/
public static function getPeriod(string $fromDate, string $toDate): array
{
return [
'start' => DateTime::toTimestamp($fromDate,DateTime::ISO8601ZULU_FORMAT),
'end' => DateTime::toTimestamp($toDate,DateTime::ISO8601ZULU_FORMAT)
];
}
}