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