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