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