Code cleanup brigage...
[squirrelmail.git] / functions / date.php
1 <?php
2
3 /**
4 * date.php
5 *
6 * Copyright (c) 1999-2001 The SquirrelMail Development 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 /*****************************************************************/
18 /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
19 /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
20 /*** + Base level indent should begin at left margin, as ***/
21 /*** the first line of the function definition below. ***/
22 /*** + All identation should consist of four space blocks ***/
23 /*** + Tab characters are evil. ***/
24 /*** + all comments should use "slash-star ... star-slash" ***/
25 /*** style -- no pound characters, no slash-slash style ***/
26 /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
27 /*** ALWAYS USE { AND } CHARACTERS!!! ***/
28 /*** + Please use ' instead of ", when possible. Note " ***/
29 /*** should always be used in _( ) function calls. ***/
30 /*** Thank you for your help making the SM code more readable. ***/
31 /*****************************************************************/
32
33 // corrects a time stamp to be the local time
34 function getGMTSeconds($stamp, $gmt) {
35 global $invert_time;
36 if (($gmt == 'Pacific') || ($gmt == 'PST'))
37 $gmt = '-0800';
38 else if (($gmt == 'EDT'))
39 $gmt = '-0400';
40 else if (($gmt == 'Eastern') || ($gmt == 'EST') || ($gmt == 'CDT'))
41 $gmt = '-0500';
42 else if (($gmt == 'Central') || ($gmt == 'CST') || ($gmt == 'MDT'))
43 $gmt = '-0600';
44 else if (($gmt == 'Mountain') || ($gmt == 'MST') || ($gmt == 'PDT'))
45 $gmt = '-0700';
46 else if ($gmt == 'BST')
47 $gmt = '+0100';
48 else if ($gmt == 'EET')
49 $gmt = '+0200';
50 else if ($gmt == 'GMT')
51 $gmt = '+0000';
52 else if ($gmt == 'HKT')
53 $gmt = '+0800';
54 else if ($gmt == 'IST')
55 $gmt = '+0200';
56 else if ($gmt == 'JST')
57 $gmt = '+0900';
58 else if ($gmt == 'KST')
59 $gmt = "+0900";
60 else if ($gmt == 'MET')
61 $gmt = '+0100';
62 else if ($gmt == 'MET DST' || $gmt == 'METDST')
63 $gmt = '+0200';
64
65 if (substr($gmt, 0, 1) == '-') {
66 $neg = true;
67 $gmt = substr($gmt, 1, strlen($gmt));
68 } else if (substr($gmt, 0, 1) == '+') {
69 $neg = false;
70 $gmt = substr($gmt, 1, strlen($gmt));
71 } else
72 $neg = false;
73
74 $gmt = substr($gmt, 0, 2);
75 $gmt = $gmt * 3600;
76 if ($neg == true)
77 $gmt = "-$gmt";
78 else
79 $gmt = "+$gmt";
80
81 /** now find what the server is at **/
82 $current = date('Z', time());
83 if ($invert_time)
84 $current = - $current;
85 $stamp = (int)$stamp - (int)$gmt + (int)$current;
86
87 return $stamp;
88 }
89
90 /**
91 Switch system has been intentionaly choosed for the
92 internationalization of month and day names. The reason
93 is to make sure that _("") strings will go into the
94 main po.
95 **/
96
97 function getDayName( $day_number ) {
98
99 switch( $day_number ) {
100 case 0:
101 $ret = _("Sunday");
102 break;
103 case 1:
104 $ret = _("Monday");
105 break;
106 case 2:
107 $ret = _("Tuesday");
108 break;
109 case 3:
110 $ret = _("Wednesday");
111 break;
112 case 4:
113 $ret = _("Thursday");
114 break;
115 case 5:
116 $ret = _("Friday");
117 break;
118 case 6:
119 $ret = _("Saturday");
120 break;
121 default:
122 $ret = '';
123 }
124 return( $ret );
125 }
126
127 function getMonthName( $month_number ) {
128 switch( $month_number ) {
129 case '01':
130 $ret = _("January");
131 break;
132 case '02':
133 $ret = _("February");
134 break;
135 case '03':
136 $ret = _("March");
137 break;
138 case '04':
139 $ret = _("April");
140 break;
141 case '05':
142 $ret = _("May");
143 break;
144 case '06':
145 $ret = _("June");
146 break;
147 case '07':
148 $ret = _("July");
149 break;
150 case '08':
151 $ret = _("August");
152 break;
153 case '09':
154 $ret = _("September");
155 break;
156 case '10':
157 $ret = _("October");
158 break;
159 case '11':
160 $ret = _("November");
161 break;
162 case '12':
163 $ret = _("December");
164 break;
165 default:
166 $ret = '';
167 }
168 return( $ret );
169 }
170
171 function date_intl( $date_format, $stamp ) {
172
173 $ret = str_replace( 'D', '$1', $date_format );
174 $ret = str_replace( 'F', '$2', $ret );
175 $ret = date( '$3'. $ret . '$3', $stamp ); // Workaround for a PHP 4.0.4 problem
176 $ret = str_replace( '$1', substr( getDayName( date( 'w', $stamp ) ), 0, 3 ), $ret );
177 $ret = str_replace( '$2', getMonthName( date( 'm', $stamp ) ), $ret );
178 $ret = str_replace( '$3', '', $ret );
179
180 return( $ret );
181 }
182
183 function getLongDateString( $stamp ) {
184
185 return( date_intl( _("D, F j, Y g:i a"), $stamp ) );
186
187 }
188
189 function getDateString( $stamp ) {
190
191 global $invert_time;
192
193 $now = time();
194
195 $dateZ = date('Z', $now );
196 if ($invert_time)
197 $dateZ = - $dateZ;
198 $midnight = $now - ($now % 86400) - $dateZ;
199
200 if ($midnight < $stamp) {
201 // Today
202 $date_format = _("g:i a");
203 } else if ($midnight - 518400 < $stamp) {
204 // This week
205 $date_format = _("D, g:i a");
206 } else {
207 // before this week
208 $date_format = _("M j, Y");
209 }
210
211 return( date_intl( $date_format, $stamp ) );
212 // return( date( $date_i, $stamp ) );
213 }
214
215 function getTimeStamp($dateParts) {
216 /** $dateParts[0] == <day of week> Mon, Tue, Wed
217 ** $dateParts[1] == <day of month> 23
218 ** $dateParts[2] == <month> Jan, Feb, Mar
219 ** $dateParts[3] == <year> 1999
220 ** $dateParts[4] == <time> 18:54:23 (HH:MM:SS)
221 ** $dateParts[5] == <from GMT> +0100
222 ** $dateParts[6] == <zone> (EDT)
223 **
224 ** NOTE: In RFC 822, it states that <day of week> is optional.
225 ** In that case, dateParts[0] would be the <day of month>
226 ** and everything would be bumped up one.
227 **/
228
229 // Simply check to see if the first element in the dateParts
230 // array is an integer or not.
231 // Since the day of week is optional, this check is needed.
232 //
233 // The old code used eregi('mon|tue|wed|thu|fri|sat|sun',
234 // $dateParts[0], $tmp) to find if the first element was the
235 // day of week or day of month. This is an expensive call
236 // (processing time) to have inside a loop. Doing it this way
237 // saves quite a bit of time for large mailboxes.
238 //
239 // It is also quicker to call explode only once rather than
240 // the 3 times it was getting called by calling the functions
241 // getHour, getMinute, and getSecond.
242 //
243 if (! isset($dateParts[1])) $dateParts[1] = '';
244 if (! isset($dateParts[2])) $dateParts[2] = '';
245 if (! isset($dateParts[3])) $dateParts[3] = '';
246 if (! isset($dateParts[4])) $dateParts[4] = '';
247 if (! isset($dateParts[5])) $dateParts[5] = '';
248 if (intval(trim($dateParts[0])) > 0) {
249 $string = $dateParts[0] . ' ' . $dateParts[1] . ' ' .
250 $dateParts[2] . ' ' . $dateParts[3];
251 return getGMTSeconds(strtotime($string), $dateParts[4]);
252 }
253 $string = $dateParts[0] . ' ' . $dateParts[1] . ' ' .
254 $dateParts[2] . ' ' . $dateParts[3] . ' ' . $dateParts[4];
255 if (isset($dateParts[5]))
256 return getGMTSeconds(strtotime($string), $dateParts[5]);
257 else
258 return getGMTSeconds(strtotime($string), '');
259 }
260
261 // I use this function for profiling. Should never be called in
262 // actual versions of squirrelmail released to public.
263 /*
264 function getmicrotime() {
265 $mtime = microtime();
266 $mtime = explode(' ',$mtime);
267 $mtime = $mtime[1] + $mtime[0];
268 return ($mtime);
269 }
270 */
271 ?>