59177427 |
1 | <?php |
7350889b |
2 | |
35586184 |
3 | /** |
4 | * date.php |
5 | * |
15e6162e |
6 | * Copyright (c) 1999-2002 The SquirrelMail Project Team |
35586184 |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
8 | * |
9 | * Takes a date and parses it into a usable format. The form that a |
10 | * date SHOULD arrive in is: |
11 | * <Tue,> 29 Jun 1999 09:52:11 -0500 (EDT) |
12 | * (as specified in RFC 822) -- 'Tue' is optional |
13 | * |
14 | * $Id$ |
15 | */ |
16 | |
0e4ae91a |
17 | require_once( '../functions/constants.php' ); |
35586184 |
18 | |
19 | // corrects a time stamp to be the local time |
20 | function getGMTSeconds($stamp, $gmt) { |
0e4ae91a |
21 | global $invert_time; |
22 | if (($gmt == 'Pacific') || ($gmt == 'PST')) { |
23 | $gmt = '-0800'; |
24 | } else if (($gmt == 'EDT')) { |
25 | $gmt = '-0400'; |
26 | } else if (($gmt == 'Eastern') || ($gmt == 'EST') || ($gmt == 'CDT')) { |
27 | $gmt = '-0500'; |
28 | } else if (($gmt == 'Central') || ($gmt == 'CST') || ($gmt == 'MDT')) { |
29 | $gmt = '-0600'; |
30 | } else if (($gmt == 'Mountain') || ($gmt == 'MST') || ($gmt == 'PDT')) { |
31 | $gmt = '-0700'; |
32 | } else if ($gmt == 'BST') { |
33 | $gmt = '+0100'; |
34 | } else if ($gmt == 'EET') { |
35 | $gmt = '+0200'; |
36 | } else if ($gmt == 'GMT') { |
37 | $gmt = '+0000'; |
38 | } else if ($gmt == 'HKT') { |
39 | $gmt = '+0800'; |
40 | } else if ($gmt == 'IST') { |
41 | $gmt = '+0200'; |
42 | } else if ($gmt == 'JST') { |
43 | $gmt = '+0900'; |
44 | } else if ($gmt == 'KST') { |
45 | $gmt = "+0900"; |
46 | } else if ($gmt == 'MET') { |
47 | $gmt = '+0100'; |
48 | } else if ($gmt == 'MET DST' || $gmt == 'METDST') { |
49 | $gmt = '+0200'; |
50 | } |
51 | |
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 | |
62 | $gmt = substr($gmt, 0, 2); |
63 | $gmt = $gmt * 3600; |
64 | if ($neg == true) { |
65 | $gmt = "-$gmt"; |
66 | } else { |
67 | $gmt = "+$gmt"; |
68 | } |
69 | |
70 | /** now find what the server is at **/ |
71 | $current = date('Z', time()); |
72 | if ($invert_time) { |
73 | $current = - $current; |
74 | } |
75 | $stamp = (int)$stamp - (int)$gmt + (int)$current; |
76 | |
77 | return $stamp; |
78 | } |
0f1835f3 |
79 | |
0e4ae91a |
80 | /** |
81 | Switch system has been intentionaly choosed for the |
82 | internationalization of month and day names. The reason |
83 | is to make sure that _("") strings will go into the |
84 | main po. |
85 | **/ |
0f1835f3 |
86 | |
0e4ae91a |
87 | function getDayName( $day_number ) { |
0f1835f3 |
88 | |
0e4ae91a |
89 | switch( $day_number ) { |
90 | case 0: |
91 | $ret = _("Sunday"); |
92 | break; |
93 | case 1: |
94 | $ret = _("Monday"); |
95 | break; |
96 | case 2: |
97 | $ret = _("Tuesday"); |
98 | break; |
99 | case 3: |
100 | $ret = _("Wednesday"); |
101 | break; |
102 | case 4: |
103 | $ret = _("Thursday"); |
104 | break; |
105 | case 5: |
106 | $ret = _("Friday"); |
107 | break; |
108 | case 6: |
109 | $ret = _("Saturday"); |
110 | break; |
111 | default: |
112 | $ret = ''; |
113 | } |
114 | return( $ret ); |
115 | } |
d59837cf |
116 | |
0e4ae91a |
117 | function getMonthName( $month_number ) { |
118 | switch( $month_number ) { |
119 | case '01': |
120 | $ret = _("January"); |
121 | break; |
122 | case '02': |
123 | $ret = _("February"); |
124 | break; |
125 | case '03': |
126 | $ret = _("March"); |
127 | break; |
128 | case '04': |
129 | $ret = _("April"); |
130 | break; |
131 | case '05': |
132 | $ret = _("May"); |
133 | break; |
134 | case '06': |
135 | $ret = _("June"); |
136 | break; |
137 | case '07': |
138 | $ret = _("July"); |
139 | break; |
140 | case '08': |
141 | $ret = _("August"); |
142 | break; |
143 | case '09': |
144 | $ret = _("September"); |
145 | break; |
146 | case '10': |
147 | $ret = _("October"); |
148 | break; |
149 | case '11': |
150 | $ret = _("November"); |
151 | break; |
152 | case '12': |
153 | $ret = _("December"); |
154 | break; |
155 | default: |
156 | $ret = ''; |
157 | } |
158 | return( $ret ); |
159 | } |
d59837cf |
160 | |
0e4ae91a |
161 | function date_intl( $date_format, $stamp ) { |
d59837cf |
162 | |
0e4ae91a |
163 | $ret = str_replace( 'D', '$1', $date_format ); |
164 | $ret = str_replace( 'F', '$2', $ret ); |
ce628f00 |
165 | $ret = str_replace( 'l', '$4', $ret ); |
0e4ae91a |
166 | $ret = date( '$3'. $ret . '$3', $stamp ); // Workaround for a PHP 4.0.4 problem |
167 | $ret = str_replace( '$1', substr( getDayName( date( 'w', $stamp ) ), 0, 3 ), $ret ); |
168 | $ret = str_replace( '$2', getMonthName( date( 'm', $stamp ) ), $ret ); |
ce628f00 |
169 | $ret = str_replace( '$4', getDayName( date( 'w', $stamp ) ), $ret ); |
0e4ae91a |
170 | $ret = str_replace( '$3', '', $ret ); |
171 | |
172 | return( $ret ); |
173 | } |
d59837cf |
174 | |
0e4ae91a |
175 | function getLongDateString( $stamp ) { |
d59837cf |
176 | |
0e4ae91a |
177 | global $hour_format; |
178 | |
179 | if ( $hour_format == SMPREF_TIME_12HR ) { |
180 | $date_format = _("D, F j, Y g:i a"); |
181 | } else { |
182 | $date_format = _("D, F j, Y G:i"); |
183 | } |
184 | |
185 | return( date_intl( $date_format, $stamp ) ); |
0f1835f3 |
186 | |
0e4ae91a |
187 | } |
d205a7bf |
188 | |
0e4ae91a |
189 | function getDateString( $stamp ) { |
d68a3926 |
190 | |
0e4ae91a |
191 | global $invert_time, $hour_format; |
192 | |
193 | $now = time(); |
194 | |
195 | $dateZ = date('Z', $now ); |
196 | if ($invert_time) { |
197 | $dateZ = - $dateZ; |
198 | } |
199 | $midnight = $now - ($now % 86400) - $dateZ; |
200 | |
201 | if ($midnight < $stamp) { |
202 | /* Today */ |
203 | if ( $hour_format == SMPREF_TIME_12HR ) { |
204 | $date_format = _("g:i a"); |
205 | } else { |
206 | $date_format = _("G:i"); |
207 | } |
208 | } else if ($midnight - 518400 < $stamp) { |
209 | /* This week */ |
210 | if ( $hour_format == SMPREF_TIME_12HR ) { |
211 | $date_format = _("D, g:i a"); |
212 | } else { |
213 | $date_format = _("D, G:i"); |
214 | } |
215 | } else { |
216 | /* before this week */ |
217 | $date_format = _("M j, Y"); |
218 | } |
219 | |
220 | return( date_intl( $date_format, $stamp ) ); |
221 | } |
0f1835f3 |
222 | |
0e4ae91a |
223 | function getTimeStamp($dateParts) { |
224 | /** $dateParts[0] == <day of week> Mon, Tue, Wed |
225 | ** $dateParts[1] == <day of month> 23 |
226 | ** $dateParts[2] == <month> Jan, Feb, Mar |
227 | ** $dateParts[3] == <year> 1999 |
228 | ** $dateParts[4] == <time> 18:54:23 (HH:MM:SS) |
229 | ** $dateParts[5] == <from GMT> +0100 |
230 | ** $dateParts[6] == <zone> (EDT) |
231 | ** |
232 | ** NOTE: In RFC 822, it states that <day of week> is optional. |
233 | ** In that case, dateParts[0] would be the <day of month> |
234 | ** and everything would be bumped up one. |
235 | **/ |
236 | |
237 | /* |
238 | * Simply check to see if the first element in the dateParts |
239 | * array is an integer or not. |
240 | * Since the day of week is optional, this check is needed. |
241 | * |
242 | * The old code used eregi('mon|tue|wed|thu|fri|sat|sun', |
243 | * $dateParts[0], $tmp) to find if the first element was the |
244 | * day of week or day of month. This is an expensive call |
245 | * (processing time) to have inside a loop. Doing it this way |
246 | * saves quite a bit of time for large mailboxes. |
247 | * |
248 | * It is also quicker to call explode only once rather than |
249 | * the 3 times it was getting called by calling the functions |
250 | * getHour, getMinute, and getSecond. |
251 | */ |
3302d0d4 |
252 | |
0e4ae91a |
253 | if (! isset($dateParts[1])) { |
254 | $dateParts[1] = ''; |
255 | } |
256 | if (! isset($dateParts[2])) { |
257 | $dateParts[2] = ''; |
258 | } |
259 | if (! isset($dateParts[3])) { |
260 | $dateParts[3] = ''; |
261 | } |
262 | if (! isset($dateParts[4])) { |
263 | $dateParts[4] = ''; |
264 | } |
265 | if (! isset($dateParts[5])) { |
266 | $dateParts[5] = ''; |
267 | } |
268 | if (intval(trim($dateParts[0])) > 0) { |
269 | $string = $dateParts[0] . ' ' . $dateParts[1] . ' ' . |
270 | $dateParts[2] . ' ' . $dateParts[3]; |
271 | return getGMTSeconds(strtotime($string), $dateParts[4]); |
272 | } |
273 | $string = $dateParts[0] . ' ' . $dateParts[1] . ' ' . |
274 | $dateParts[2] . ' ' . $dateParts[3] . ' ' . $dateParts[4]; |
275 | if (isset($dateParts[5])) { |
276 | return getGMTSeconds(strtotime($string), $dateParts[5]); |
277 | } else { |
278 | return getGMTSeconds(strtotime($string), ''); |
279 | } |
280 | } |
5e90d34a |
281 | |
0e4ae91a |
282 | /* I use this function for profiling. Should never be called in |
283 | actual versions of squirrelmail released to public. */ |
d205a7bf |
284 | /* |
5e90d34a |
285 | function getmicrotime() { |
286 | $mtime = microtime(); |
b9bfd165 |
287 | $mtime = explode(' ',$mtime); |
5e90d34a |
288 | $mtime = $mtime[1] + $mtime[0]; |
289 | return ($mtime); |
9774bdef |
290 | } |
d205a7bf |
291 | */ |
35586184 |
292 | ?> |