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