decoding filenames in download.php as well
[squirrelmail.git] / functions / date.php
CommitLineData
59177427 1<?php
3302d0d4 2 /**
a09387f4 3 ** date.php
3302d0d4 4 **
5 ** Takes a date and parses it into a usable format. The form that a
6 ** date SHOULD arrive in is:
5b10f02a 7 ** <Tue,> 29 Jun 1999 09:52:11 -0500 (EDT)
8 ** (as specified in RFC 822) -- "Tue" is optional
3302d0d4 9 **
10 **/
11
d068c0ec 12 $date_php = true;
13
aceb0d5c 14 function getMinutes($hour) {
15 $date = $hour;
16
17 if (($hour == 0) || ($hour == "00"))
18 $date = "00";
19 else if (($hour == 1) || ($hour == "01"))
20 $date = "01";
21 else if (($hour == 2) || ($hour == "02"))
22 $date = "02";
23 else if (($hour == 3) || ($hour == "03"))
24 $date = "03";
25 else if (($hour == 4) || ($hour == "04"))
26 $date = "04";
27 else if (($hour == 5) || ($hour == "05"))
28 $date = "05";
29 else if (($hour == 6) || ($hour == "06"))
30 $date = "06";
31 else if (($hour == 7) || ($hour == "07"))
32 $date = "07";
33 else if (($hour == 8) || ($hour == "08"))
34 $date = "08";
35 else if (($hour == 9) || ($hour == "09"))
36 $date = "09";
37
38 return $date;
39 }
40
0f1835f3 41 // corrects a time stamp to be the local time
42 function getGMTSeconds($stamp, $gmt) {
ddd32bad 43 if (($gmt == "Pacific") || ($gmt == "PST") || ($gmt == "PDT"))
44 $gmt = "-0800";
45 if (($gmt == "Eastern") || ($gmt == "EST") || ($gmt == "EDT"))
46 $gmt = "-0500";
47 if (($gmt == "Central") || ($gmt == "CST") || ($gmt == "CDT"))
48 $gmt = "-0600";
49 if (($gmt == "Mountain") || ($gmt == "MST") || ($gmt == "MDT"))
50 $gmt = "-0700";
51
0f1835f3 52 if (substr($gmt, 0, 1) == "-") {
53 $neg = true;
54 $gmt = substr($gmt, 1, strlen($gmt));
55 } else if (substr($gmt, 0, 1) == "+") {
56 $neg = false;
57 $gmt = substr($gmt, 1, strlen($gmt));
58 } else
59 $neg = false;
60
61 $gmt = substr($gmt, 0, 2);
62 $gmt = $gmt * 3600;
63 if ($neg == true)
64 $gmt = "-$gmt";
65 else
66 $gmt = "+$gmt";
67
68 /** now find what the server is at **/
69 $current = date("Z", time());
70
71 $stamp = (int)$stamp - (int)$gmt + (int)$current;
72
73 return $stamp;
74 }
75
3302d0d4 76 function getHour($hour) {
77 $time = explode(":", $hour);
78 return $time[0];
79 }
80
81 function getMinute($min) {
82 $time = explode(":", $min);
83 return $time[1];
84 }
85
86 function getSecond($sec) {
87 $time = explode(":", $sec);
88 return $time[2];
89 }
90
91 function getMonthNum($month) {
92 if (eregi("jan|january", $month, $tmp))
93 $date = "01";
94 else if (eregi("feb|february|febuary", $month, $tmp))
95 $date = "02";
96 else if (eregi("mar|march", $month, $tmp))
97 $date = "03";
98 else if (eregi("apr|april", $month, $tmp))
99 $date = "04";
100 else if (eregi("may", $month, $tmp))
101 $date = "05";
102 else if (eregi("jun|june", $month, $tmp))
103 $date = "06";
104 else if (eregi("jul|july", $month, $tmp))
105 $date = "07";
106 else if (eregi("aug|august", $month, $tmp))
107 $date = "08";
108 else if (eregi("sep|sept|september", $month, $tmp))
109 $date = "09";
110 else if (eregi("oct|october", $month, $tmp))
111 $date = "10";
112 else if (eregi("nov|november", $month, $tmp))
113 $date = "11";
114 else if (eregi("dec|december", $month, $tmp))
115 $date = "12";
116
117 return $date;
118 }
119
120 function getDayOfWeek($day) {
121 $date = "{WEEKDAY}";
122
123 if (eregi("(mon|monday)", $day, $tmp))
124 $date = "Mon";
125 else if (eregi("(tue|tuesday)", $day, $tmp))
126 $date = "Tue";
127 else if (eregi("(wed|wednesday)", $day, $tmp))
128 $date = "Wed";
129 else if (eregi("(thurs|thu|thursday)", $day, $tmp))
130 $date = "Thu";
131 else if (eregi("(fri|friday)", $day, $tmp))
132 $date = "Fri";
133 else if (eregi("(sat|saturday)", $day, $tmp))
134 $date = "Sat";
135 else if (eregi("(sun|sunday)", $day, $tmp))
136 $date = "Sun";
137
138 return $date;
139 }
140
141 function getDayOfMonth($day) {
142 return ereg_replace("^0", "", $day); /* remove a preceeding 0 */
143 }
144
145 function getMonth($month) {
146 $date = "{MONTH}";
147 if (eregi("jan|january", $month, $tmp))
148 $date = "Jan";
149 else if (eregi("feb|february|febuary", $month, $tmp))
150 $date = "Feb";
151 else if (eregi("mar|march", $month, $tmp))
152 $date = "Mar";
153 else if (eregi("apr|april", $month, $tmp))
154 $date = "Apr";
155 else if (eregi("may", $month, $tmp))
156 $date = "May";
157 else if (eregi("jun|june", $month, $tmp))
158 $date = "Jun";
159 else if (eregi("jul|july", $month, $tmp))
160 $date = "Jul";
161 else if (eregi("aug|august", $month, $tmp))
162 $date = "Aug";
163 else if (eregi("sep|sept|september", $month, $tmp))
164 $date = "Sep";
165 else if (eregi("oct|october", $month, $tmp))
166 $date = "Oct";
167 else if (eregi("nov|november", $month, $tmp))
168 $date = "Nov";
169 else if (eregi("dec|december", $month, $tmp))
170 $date = "Dec";
171
172 return $date;
173 }
174
175 function getYear($year) {
176 return $year;
177 }
178
0f1835f3 179 function getLongDateString($stamp) {
180 return date("D, F j, Y g:i a", $stamp);
181 }
182
183 function getDateString($stamp) {
d68a3926 184 $now = time();
185 $midnight = $now - ($now % 86400) + 14400;
186
187 if ($midnight < $stamp) {
188 // Today
189 return date("g:i a", $stamp);
190 } else if ($midnight - 604800 < $stamp) {
191 // This week
192 return date("D, g:i a", $stamp);
193 } else {
194 // before this week
195 return date("M j, Y", $stamp);
196 }
0f1835f3 197 }
198
199 function getTimeStamp($dateParts) {
3302d0d4 200 /** $dateParts[0] == <day of week> Mon, Tue, Wed
201 ** $dateParts[1] == <day of month> 23
202 ** $dateParts[2] == <month> Jan, Feb, Mar
203 ** $dateParts[3] == <year> 1999
204 ** $dateParts[4] == <time> 18:54:23 (HH:MM:SS)
205 ** $dateParts[5] == <from GMT> +0100
206 ** $dateParts[6] == <zone> (EDT)
207 **
208 ** NOTE: In RFC 822, it states that <day of week> is optional.
209 ** In that case, dateParts[0] would be the <day of month>
210 ** and everything would be bumped up one.
211 **/
212
d068c0ec 213 // Simply check to see if the first element in the dateParts
214 // array is an integer or not.
5e90d34a 215 // Since the day of week is optional, this check is needed.
216 //
d068c0ec 217 // The old code used eregi("mon|tue|wed|thu|fri|sat|sun",
218 // $dateParts[0], $tmp) to find if the first element was the
219 // day of week or day of month. This is an expensive call
220 // (processing time) to have inside a loop. Doing it this way
221 // saves quite a bit of time for large mailboxes.
5e90d34a 222 //
d068c0ec 223 // It is also quicker to call explode only once rather than
224 // the 3 times it was getting called by calling the functions
225 // getHour, getMinute, and getSecond.
5e90d34a 226 //
227 if (intval(trim($dateParts[0])) > 0) {
228 $time = explode(":", $dateParts[3]);
229 $d[0] = $time[0];
230 $d[1] = $time[1];
231 $d[2] = $time[2];
232 $d[3] = getMonthNum(trim($dateParts[1]));
233 $d[4] = getDayOfMonth(trim($dateParts[0]));
234 $d[5] = getYear(trim($dateParts[2]));
235 return getGMTSeconds(mktime($d[0], $d[1], $d[2], $d[3], $d[4], $d[5]), $dateParts[4]);
5b10f02a 236 }
5e90d34a 237 $time = explode(":", $dateParts[4]);
238 $d[0] = $time[0];
239 $d[1] = $time[1];
240 $d[2] = $time[2];
241 $d[3] = getMonthNum(trim($dateParts[2]));
242 $d[4] = getDayOfMonth(trim($dateParts[1]));
243 $d[5] = getYear(trim($dateParts[3]));
244 return getGMTSeconds(mktime($d[0], $d[1], $d[2], $d[3], $d[4], $d[5]), $dateParts[5]);
245 }
246
d068c0ec 247 // I use this function for profiling. Should never be called in
248 // actual versions of squirrelmail released to public.
5e90d34a 249 function getmicrotime() {
250 $mtime = microtime();
251 $mtime = explode(" ",$mtime);
252 $mtime = $mtime[1] + $mtime[0];
253 return ($mtime);
9774bdef 254 }
3302d0d4 255?>