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