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