If date can not be parsed, do not display date header at all (better than wrong date...
[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
19// corrects a time stamp to be the local time
20function getGMTSeconds($stamp, $gmt) {
0e4ae91a 21 global $invert_time;
913ed9a3 22
23 // date couldn't be parsed
24 if ($stamp == -1) {
25 return -1;
26 }
27
0e4ae91a 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 }
5c2790ca 67
68 $difference = substr($gmt, 2, 2);
0e4ae91a 69 $gmt = substr($gmt, 0, 2);
5c2790ca 70 $gmt = ($gmt + ($difference / 60)) * 3600;
0e4ae91a 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}
0f1835f3 86
0e4ae91a 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**/
0f1835f3 93
0e4ae91a 94function getDayName( $day_number ) {
0f1835f3 95
0e4ae91a 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}
d59837cf 123
0e4ae91a 124function 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}
d59837cf 167
0e4ae91a 168function date_intl( $date_format, $stamp ) {
d59837cf 169
0e4ae91a 170 $ret = str_replace( 'D', '$1', $date_format );
171 $ret = str_replace( 'F', '$2', $ret );
ce628f00 172 $ret = str_replace( 'l', '$4', $ret );
60087c97 173 $ret = str_replace( 'M', '$5', $ret );
0e4ae91a 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 );
60087c97 176 $ret = str_replace( '$5', substr( getMonthName( date( 'm', $stamp ) ), 0, 3 ), $ret );
0e4ae91a 177 $ret = str_replace( '$2', getMonthName( date( 'm', $stamp ) ), $ret );
ce628f00 178 $ret = str_replace( '$4', getDayName( date( 'w', $stamp ) ), $ret );
0e4ae91a 179 $ret = str_replace( '$3', '', $ret );
180
181 return( $ret );
182}
d59837cf 183
0e4ae91a 184function getLongDateString( $stamp ) {
d59837cf 185
0e4ae91a 186 global $hour_format;
187
913ed9a3 188 if ($stamp == -1) {
189 return '';
190 }
191
0e4ae91a 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 ) );
0f1835f3 199
0e4ae91a 200}
d205a7bf 201
0e4ae91a 202function getDateString( $stamp ) {
d68a3926 203
0e4ae91a 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}
0f1835f3 235
0e4ae91a 236function 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 */
3302d0d4 265
0e4ae91a 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}
5e90d34a 294
0e4ae91a 295/* I use this function for profiling. Should never be called in
296 actual versions of squirrelmail released to public. */
d205a7bf 297/*
5e90d34a 298 function getmicrotime() {
299 $mtime = microtime();
b9bfd165 300 $mtime = explode(' ',$mtime);
5e90d34a 301 $mtime = $mtime[1] + $mtime[0];
302 return ($mtime);
9774bdef 303 }
d205a7bf 304*/
35586184 305?>