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