phpdocumentor basic preparation, continued
[squirrelmail.git] / functions / date.php
CommitLineData
59177427 1<?php
7350889b 2
35586184 3/**
4 * date.php
5 *
76911253 6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
35586184 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$
d6c32258 15 * @package squirrelmail
35586184 16 */
17
d6c32258 18/** Load up some useful constants */
b68edc75 19require_once(SM_PATH . 'functions/constants.php');
35586184 20
d6c32258 21/** corrects a time stamp to be the local time */
d5c4ae7c 22function getGMTSeconds($stamp, $tzc) {
dc06f88c 23 /* date couldn't be parsed */
913ed9a3 24 if ($stamp == -1) {
25 return -1;
26 }
d5c4ae7c 27 /* timezone correction, expressed as `shhmm' */
28 switch($tzc)
dc06f88c 29 {
30 case 'Pacific':
31 case 'PST':
d5c4ae7c 32 $tzc = '-0800';
dc06f88c 33 break;
34 case 'Mountain':
35 case 'MST':
36 case 'PDT':
d5c4ae7c 37 $tzc = '-0700';
dc06f88c 38 break;
39 case 'Central':
40 case 'CST':
41 case 'MDT':
d5c4ae7c 42 $tzc = '-0600';
dc06f88c 43 break;
44 case 'Eastern':
45 case 'EST':
46 case 'CDT':
d5c4ae7c 47 $tzc = '-0500';
dc06f88c 48 break;
49 case 'EDT':
d5c4ae7c 50 $tzc = '-0400';
dc06f88c 51 break;
52 case 'GMT':
d5c4ae7c 53 $tzc = '+0000';
dc06f88c 54 break;
55 case 'BST':
d5c4ae7c 56 case 'MET':
57 case 'CET':
58 $tzc = '+0100';
59 break;
dc06f88c 60 case 'EET':
61 case 'IST':
62 case 'MET DST':
63 case 'METDST':
d5c4ae7c 64 $tzc = '+0200';
dc06f88c 65 break;
66 case 'HKT':
d5c4ae7c 67 $tzc = '+0800';
dc06f88c 68 break;
69 case 'JST':
70 case 'KST':
d5c4ae7c 71 $tzc = '+0900';
dc06f88c 72 break;
0e4ae91a 73 }
d5c4ae7c 74 $neg = false;
75 if (substr($tzc, 0, 1) == '-') {
0e4ae91a 76 $neg = true;
d5c4ae7c 77 } else if (substr($tzc, 0, 1) != '+') {
78 $tzc = '+'.$tzc;
0e4ae91a 79 }
d5c4ae7c 80 $hh = substr($tzc,1,2);
81 $mm = substr($tzc,3,2);
82 $iTzc = ($hh * 60 + $mm) * 60;
83 if ($neg) $iTzc = -1 * (int) $iTzc;
84 /* stamp in gmt */
85 $stamp -= $iTzc;
0e4ae91a 86 /** now find what the server is at **/
87 $current = date('Z', time());
d5c4ae7c 88 /* stamp in local timezone */
89 $stamp += $current;
0e4ae91a 90
91 return $stamp;
92}
0f1835f3 93
0e4ae91a 94/**
dc06f88c 95 Switch system has been intentionaly chosen for the
0e4ae91a 96 internationalization of month and day names. The reason
97 is to make sure that _("") strings will go into the
98 main po.
99**/
0f1835f3 100
0e4ae91a 101function getDayName( $day_number ) {
0f1835f3 102
0e4ae91a 103 switch( $day_number ) {
104 case 0:
105 $ret = _("Sunday");
106 break;
107 case 1:
108 $ret = _("Monday");
109 break;
110 case 2:
111 $ret = _("Tuesday");
112 break;
113 case 3:
114 $ret = _("Wednesday");
115 break;
116 case 4:
117 $ret = _("Thursday");
118 break;
119 case 5:
120 $ret = _("Friday");
121 break;
122 case 6:
123 $ret = _("Saturday");
124 break;
125 default:
126 $ret = '';
127 }
128 return( $ret );
129}
d59837cf 130
22d73941 131function getDayAbrv( $day_number ) {
132
133 switch( $day_number ) {
134 case 0:
135 $ret = _("Sun");
136 break;
137 case 1:
138 $ret = _("Mon");
139 break;
140 case 2:
141 $ret = _("Tue");
142 break;
143 case 3:
144 $ret = _("Wed");
145 break;
146 case 4:
147 $ret = _("Thu");
148 break;
149 case 5:
150 $ret = _("Fri");
151 break;
152 case 6:
153 $ret = _("Sat");
154 break;
155 default:
156 $ret = '';
157 }
158 return( $ret );
159}
160
0e4ae91a 161function getMonthName( $month_number ) {
162 switch( $month_number ) {
163 case '01':
164 $ret = _("January");
165 break;
166 case '02':
167 $ret = _("February");
168 break;
169 case '03':
170 $ret = _("March");
171 break;
172 case '04':
173 $ret = _("April");
174 break;
175 case '05':
176 $ret = _("May");
177 break;
178 case '06':
179 $ret = _("June");
180 break;
181 case '07':
182 $ret = _("July");
183 break;
184 case '08':
185 $ret = _("August");
186 break;
187 case '09':
188 $ret = _("September");
189 break;
190 case '10':
191 $ret = _("October");
192 break;
193 case '11':
194 $ret = _("November");
195 break;
196 case '12':
197 $ret = _("December");
198 break;
199 default:
200 $ret = '';
201 }
202 return( $ret );
203}
d59837cf 204
22d73941 205function getMonthAbrv( $month_number ) {
206 switch( $month_number ) {
207 case '01':
208 $ret = _("Jan");
209 break;
210 case '02':
211 $ret = _("Feb");
212 break;
213 case '03':
214 $ret = _("Mar");
215 break;
216 case '04':
217 $ret = _("Apr");
218 break;
219 case '05':
220 $ret = _("Ma&#121;");
221 break;
222 case '06':
223 $ret = _("Jun");
224 break;
225 case '07':
226 $ret = _("Jul");
227 break;
228 case '08':
229 $ret = _("Aug");
230 break;
231 case '09':
232 $ret = _("Sep");
233 break;
234 case '10':
235 $ret = _("Oct");
236 break;
237 case '11':
238 $ret = _("Nov");
239 break;
240 case '12':
241 $ret = _("Dec");
242 break;
243 default:
244 $ret = '';
245 }
246 return( $ret );
247}
248
249
0e4ae91a 250function date_intl( $date_format, $stamp ) {
e6b76811 251 $ret = str_replace( array('D','F','l','M'), array('$1','$2','$3','$4'), $date_format );
252 $ret = date('w#m#'. $ret, $stamp ); // to reduce the date calls we retrieve m and w in the same call
253 $aParts = explode('#',$ret); // extract day and month in order to replace later by intl day and month
254 $ret = str_replace(array('$1','$4','$2','$3',), array(getDayAbrv($aParts[0]),
15d49d77 255 getMonthAbrv($aParts[1]),
256 getMonthName($aParts[1]),
e6b76811 257 getDayName($aParts[0])),
258 $aParts[2]);
259 return( $ret );
0e4ae91a 260}
d59837cf 261
0e4ae91a 262function getLongDateString( $stamp ) {
d59837cf 263
0e4ae91a 264 global $hour_format;
265
913ed9a3 266 if ($stamp == -1) {
267 return '';
268 }
269
0e4ae91a 270 if ( $hour_format == SMPREF_TIME_12HR ) {
271 $date_format = _("D, F j, Y g:i a");
272 } else {
273 $date_format = _("D, F j, Y G:i");
274 }
275
276 return( date_intl( $date_format, $stamp ) );
0f1835f3 277
0e4ae91a 278}
d205a7bf 279
0e4ae91a 280function getDateString( $stamp ) {
d68a3926 281
3e3b60e3 282 global $invert_time, $hour_format, $show_full_date;
dc06f88c 283
284 if ( $stamp == -1 ) {
285 return '';
286 }
0e4ae91a 287
288 $now = time();
289
290 $dateZ = date('Z', $now );
291 if ($invert_time) {
292 $dateZ = - $dateZ;
293 }
294 $midnight = $now - ($now % 86400) - $dateZ;
7e27023f 295 $nextmid = $midnight + 86400;
0e4ae91a 296
7e27023f 297 if (($show_full_date == 1) || ($nextmid < $stamp)) {
3e3b60e3 298 $date_format = _("M j, Y");
299 } else if ($midnight < $stamp) {
0e4ae91a 300 /* Today */
301 if ( $hour_format == SMPREF_TIME_12HR ) {
302 $date_format = _("g:i a");
303 } else {
304 $date_format = _("G:i");
305 }
306 } else if ($midnight - 518400 < $stamp) {
307 /* This week */
308 if ( $hour_format == SMPREF_TIME_12HR ) {
309 $date_format = _("D, g:i a");
310 } else {
311 $date_format = _("D, G:i");
312 }
313 } else {
314 /* before this week */
315 $date_format = _("M j, Y");
316 }
317
318 return( date_intl( $date_format, $stamp ) );
319}
0f1835f3 320
0e4ae91a 321function getTimeStamp($dateParts) {
322 /** $dateParts[0] == <day of week> Mon, Tue, Wed
323 ** $dateParts[1] == <day of month> 23
324 ** $dateParts[2] == <month> Jan, Feb, Mar
325 ** $dateParts[3] == <year> 1999
326 ** $dateParts[4] == <time> 18:54:23 (HH:MM:SS)
327 ** $dateParts[5] == <from GMT> +0100
328 ** $dateParts[6] == <zone> (EDT)
329 **
330 ** NOTE: In RFC 822, it states that <day of week> is optional.
331 ** In that case, dateParts[0] would be the <day of month>
332 ** and everything would be bumped up one.
333 **/
d5c4ae7c 334
0e4ae91a 335 /*
336 * Simply check to see if the first element in the dateParts
337 * array is an integer or not.
dc06f88c 338 * Since the day of week is optional, this check is needed.
0e4ae91a 339 */
50457d45 340 if (count($dateParts) <2) {
341 return 0;
342 }
3302d0d4 343
d5c4ae7c 344 /* remove day of week */
345 if (!is_numeric(trim($dateParts[0]))) {
346 $dataParts = array_shift($dateParts);
0e4ae91a 347 }
d5c4ae7c 348 /* calculate timestamp separated from the zone and obs-zone */
349 $stamp = strtotime(implode (' ', array_splice ($dateParts,0,4)));
350 if (!isset($dateParts[0])) {
351 $dateParts[0] = '+0000';
0e4ae91a 352 }
dc06f88c 353
d5c4ae7c 354 if (!preg_match('/^[+-]{1}[0-9]{4}$/',$dateParts[0])) {
355 /* zone in obs-zone format */
356 if (preg_match('/\((.+)\)/',$dateParts[0],$regs)) {
357 $obs_zone = $regs[1];
358 } else {
359 $obs_zone = $dateParts[0];
360 }
361 return getGMTSeconds($stamp, $obs_zone);
362 } else {
363 return getGMTSeconds($stamp, $dateParts[0]);
0e4ae91a 364 }
0e4ae91a 365}
5e90d34a 366
0e4ae91a 367/* I use this function for profiling. Should never be called in
368 actual versions of squirrelmail released to public. */
d205a7bf 369/*
5e90d34a 370 function getmicrotime() {
371 $mtime = microtime();
b9bfd165 372 $mtime = explode(' ',$mtime);
5e90d34a 373 $mtime = $mtime[1] + $mtime[0];
374 return ($mtime);
9774bdef 375 }
d205a7bf 376*/
35586184 377?>