-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdates.php
246 lines (216 loc) · 5.96 KB
/
dates.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
class Dates {
/**
* Displays a dropdown list containing the days 1 - 31 with the current day selected
* @category Dates
* <code>
* echo Dates::dropdownDay();
* </code>
*/
function dropdownDay() {
try {
$currentDay = date("d");
echo "<select name='day'>";
for ($day = 1; $day <= 31; $day++) {
if ($day == $currentDay) {
echo "<option selected='selected'>" . $day . "</option>" . "\n";
} else {
echo "<option>" . $day . "</option>" . "\n";
}
}
echo "</select>";
} catch (Exception $err) {
return $err->getMessage();
}
}
/**
* Displays a dropdown list containing the months 1 - 12 with the current month selected
* @category Dates
* <code>
* echo Dates::dropdownMonth();
* </code>
*/
function dropdownMonth() {
try {
$currentMonth = date("m");
echo "<select name='month'>";
for ($month = 1; $month <= 12; $month++) {
if ($month == $currentMonth) {
echo "<option selected='selected'>" . $month . "</option>" . "\n";
} else {
echo "<option>" . $month . "</option>" . "\n";
}
}
echo "</select>";
} catch (Exception $err) {
return $err->getMessage();
}
}
/**
* Displays a dropdown list containing the starting and ending year that is specified
* @category Dates
* <code>
* echo Dates::dropdownYear(2000, 2014);
* </code>
*/
function dropdownYear($startYear, $endYear) {
try {
$startYear = ($startYear) ? $startYear - 1 : date('Y') - 10;
$endYear = ($endYear) ? $endYear : date('Y');
echo "<select name='year'>";
for ($i = $endYear; $i > $startYear; $i -= 1) {
echo "<option>{$i}</option>";
}
echo "</select>";
} catch (Exception $err) {
return $err->getMessage();
}
}
/**
* Gets the last day of the month specified
* @param <integer> $monthNum (optional) Month number to check (01 - 12). Leave blank for current month
* @param <integer> $year (optional) Year to check e.g. 2012. Leave blank for current year
* @return <string> In format yyyy-mm-dd
* @category Dates
* <code>
* $result = Dates::getMonthLastday(1, 2014);
* </code>
*/
function getMonthLastday($monthNum = NULL, $year = NULL) {
try {
$monthNum = ($monthNum) ? $monthNum : date('m');
$year = ($year) ? $year : date('Y');
return date('Y-m-d', strtotime('-1 second', strtotime('+1 month', strtotime($monthNum . '/01/' . $year . ' 00:00:00'))));
} catch (Exception $err) {
return $err->getMessage();
}
}
/**
* Calculates the difference between two dates in days, months and years
* @param <string> $startDate Starting date in format yyyy/mm/dd
* @param <string> $endDate Ending date in format yyyy/mm/dd
* @return <string> Specifying the days, months and years in difference
* @category Dates
* <code>
* $result = Dates::getDateDifference('2014/02/01', '2014/02/02');
* </code>
*/
function getDateDifference($startDate, $endDate) {
try {
$diff = abs(strtotime($endDate) - strtotime($startDate));
$years = floor($diff / (365 * 60 * 60 * 24));
$months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
$days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
return "{$days} days, {$months} months, {$years} years";
} catch (Exception $err) {
return $err->getMessage();
}
}
/**
* Calculates the amount of time in words
* @param <integer> $seconds Total amount of seconds to convert to words
* @return <string> The amount of time in words
* @category Dates
* <code>
* $result = Dates::secondsToWords(123456789);
* </code>
*/
function secondsToWords($seconds) {
try {
$ret = "";
// calculating the days
$days = intval(intval($seconds) / (3600 * 24));
if ($days > 0) {
$ret .= "$days days ";
}
// calulating the hours
$hours = (intval($seconds) / 3600) % 24;
if ($hours > 0) {
$ret .= "$hours hours ";
}
// calulating the minutes
$minutes = (intval($seconds) / 60) % 60;
if ($minutes > 0) {
$ret .= "$minutes minutes ";
}
// calulating the seconds
$seconds = intval($seconds) % 60;
if ($seconds > 0) {
$ret .= "$seconds seconds";
}
// returning the formatted string
return $ret;
} catch (Exception $err) {
return $err->getMessage();
}
}
/**
* Auto updates the copyright dates depending on tha start and current year
* @param <integer> $startYear Year to start calculate from
* @return <string> Copyright date
* @category Dates
* <code>
* $result = Dates::autoUpdateCopyright(2010);
* </code>
*/
function autoUpdateCopyright($startYear) {
try {
// given start year (e.g. 2004)
$startYear = intval($startYear);
// current year (e.g. 2007)
$year = intval(date('Y'));
// is the current year greater than the
// given start year?
if ($year > $startYear) {
return $startYear . ' - ' . $year;
} else {
return $startYear;
}
} catch (Exception $err) {
return $err->getMessage();
}
}
/**
* Converts the given seconds into an array in the different tme formats
* @param <integer> $time Seconds to calculate
* @return <array> Array containing time elements
* @category Dates
* <code>
* $result = Dates::secondsToTimeArray(699999);
* </code>
*/
function secondsToTimeArray($time) {
try {
if (is_numeric($time)) {
$value = array(
"years" => 0, "days" => 0, "hours" => 0,
"minu"
. "tes" => 0, "seconds" => 0,
);
if ($time >= 31556926) {
$value["years"] = floor($time / 31556926);
$time = ($time % 31556926);
}
if ($time >= 86400) {
$value["days"] = floor($time / 86400);
$time = ($time % 86400);
}
if ($time >= 3600) {
$value["hours"] = floor($time / 3600);
$time = ($time % 3600);
}
if ($time >= 60) {
$value["minutes"] = floor($time / 60);
$time = ($time % 60);
}
$value["seconds"] = floor($time);
return $value;
} else {
return FALSE;
}
} catch (Exception $err) {
return $err->getMessage();
}
}
}
?>