-
Notifications
You must be signed in to change notification settings - Fork 0
/
Time.php
188 lines (170 loc) · 4.63 KB
/
Time.php
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
class Util_Time
{
/**
* ...
*
* @param mixed $now
* @return numeric
*/
public static function getToday($now = null)
{
$time = self::_time($now ?: time());
return strtotime(date("Y-m-d", $time));
}
/**
*
* @param mixed $now
* @return numeric
*/
public static function getNextBusinessDay($now = null)
{
$time = self::_time($now ?: time());
if (date('D', $time) == 'Fri' || date('D', $time) == 'Sat') {
return strtotime('next Monday 12:00 AM', $time);
} else {
return strtotime('tomorrow 12:00 AM', $time);
}
}
/**
* Returns the difference in seconds between 2 provided timestamps
* @todo This should be moved into a helper method when we start to port Util Methods
*
* @param $time
* @return int
*/
public function getTimeDifference($time1, $time2)
{
if ($time1 > $time2) {
$timeDifference = ($time2 - $time1) * -1;
} elseif ($time1 < $time2) {
$timeDifference = $time2 - $time1;
} else {
return false;
}
return $timeDifference;
}
/**
* Returns a formatted difference in time as a string (must provide time in seconds)
* @todo This should be moved into a helper method when we start to port Util Methods
*
* @param $timeInSeconds
* @return string
*/
public function formatDisplayTime($timeInSeconds)
{
$timeInMinutes = floor($timeInSeconds / 60);
$timeInHours = floor($timeInSeconds / 3600);
$timeInDays = floor($timeInSeconds / 86400);
if ($timeInMinutes < 60) {
$timeString = ($timeInMinutes == 1 ? $timeInMinutes . ' MINUTE' : $timeInMinutes . ' MINUTES');
} elseif ($timeInMinutes < 1440) {
$timeString = ($timeInHours > 1 ? $timeInHours . ' HOURS' : $timeInHours . ' HOUR');
} else {
$timeString = ($timeInDays == 1 ? $timeInDays . ' DAY' : $timeInDays . ' DAYS');
}
return $timeString;
}
/**
* ...
*
* @param mixed $time
* @return integer
*/
public static function getDayStart($time = null)
{
$time = self::_time($time ?: time());
return strtotime(date("Y-m-d", $time));
}
// can always -1 for getDayEnd
public static function getNextDayStart($time = null)
{
$time = self::_time($time ?: time());
// +1 day -> normalize that to "YYY-mm-dd" -> convert back to ts
return strtotime(date("Y-m-d", strtotime('+1 day', $time)));
}
/**
*
* @param mixed $now
* @param string $day
* @return numeric
*/
static function getWeekStart($now = null, $day = 'Sun')
{
$week_start = self::getToday($now);
while (date('D', $week_start) != $day) {
$week_start = strtotime('-1 day', $week_start);
}
return $week_start;
}
/**
* ...
*
* @param mixed $now
* @return numeric
*/
static function getMonthStart($now = null)
{
$time = self::_time($now ?: time());
return strtotime(date("Y-m-01", $time));
}
/**
* ...
*
* @param mixed $now
* @return numeric
*/
public static function roundHours($now = null)
{
$time = $now ?: time();
return strtotime(date('Y-m-d H:00:00', $time));
}
/**
* ...
*
* @param mixed $now
* @return numeric
*/
public static function roundMinutes($now = null, $min = 1)
{
$time = $now ?: time();
$m = floor(date('i', $time) / $min) * $min;
return strtotime(date("Y-m-d H:{$m}:00", $time));
}
/**
* Returns an array containing strftime style times across a time range
*
* @param string $strftime time format to fill array with
* @param int start timestamp
* @param int end timestamp
* @param string increment size in style, parseable by strtotime
* @return array of times within the range
*/
public static function getFormattedDateRange($format, $start, $end, $increment)
{
$rows = array();
for ($time = $start; $time < $end; $time = strtotime($increment, $time)) {
$rows[] = strftime($format, $time);
}
return $rows;
}
//** Private
/**
* Converts input to unix timestamp
*
* @param mixed $obj
* @return numeric|false
*/
private static function _time($obj = null)
{
if (! $obj) {
return time();
} else if ($obj instanceof MongoDate) {
return $obj->sec;
} else if (is_integer($obj)) {
return $obj;
} else {
return strtotime((string) $obj);
}
}
}