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