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