59177427 |
1 | <?php |
3302d0d4 |
2 | /** |
a09387f4 |
3 | ** date.php |
3302d0d4 |
4 | ** |
2ba13803 |
5 | ** Copyright (c) 1999-2001 The Squirrelmail Development Team |
6 | ** Licensed under the GNU GPL. For full terms see the file COPYING. |
7 | ** |
3302d0d4 |
8 | ** Takes a date and parses it into a usable format. The form that a |
9 | ** date SHOULD arrive in is: |
5b10f02a |
10 | ** <Tue,> 29 Jun 1999 09:52:11 -0500 (EDT) |
b9bfd165 |
11 | ** (as specified in RFC 822) -- 'Tue' is optional |
3302d0d4 |
12 | ** |
245a6892 |
13 | ** $Id$ |
3302d0d4 |
14 | **/ |
15 | |
0f1835f3 |
16 | // corrects a time stamp to be the local time |
17 | function getGMTSeconds($stamp, $gmt) { |
2ed6f907 |
18 | global $invert_time; |
b9bfd165 |
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'; |
f435778e |
41 | else if ($gmt == 'KST') |
42 | $gmt = "+0900"; |
b9bfd165 |
43 | else if ($gmt == 'MET') |
44 | $gmt = '+0100'; |
45 | else if ($gmt == 'MET DST' || $gmt == 'METDST') |
46 | $gmt = '+0200'; |
47654bfb |
47 | |
b9bfd165 |
48 | if (substr($gmt, 0, 1) == '-') { |
0f1835f3 |
49 | $neg = true; |
50 | $gmt = substr($gmt, 1, strlen($gmt)); |
b9bfd165 |
51 | } else if (substr($gmt, 0, 1) == '+') { |
0f1835f3 |
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 **/ |
b9bfd165 |
65 | $current = date('Z', time()); |
d47b2518 |
66 | if ($invert_time) |
67 | $current = - $current; |
0f1835f3 |
68 | $stamp = (int)$stamp - (int)$gmt + (int)$current; |
69 | |
70 | return $stamp; |
71 | } |
72 | |
ca5b5e00 |
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 | **/ |
d59837cf |
79 | |
80 | function getDayName( $day_number ) { |
81 | |
ca5b5e00 |
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 ); |
d59837cf |
108 | } |
109 | |
ca5b5e00 |
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 | } |
d59837cf |
153 | |
ca5b5e00 |
154 | function date_intl( $date_format, $stamp ) { |
d59837cf |
155 | |
d205a7bf |
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 ); |
ca5b5e00 |
164 | } |
d59837cf |
165 | |
d205a7bf |
166 | function getLongDateString( $stamp ) { |
d59837cf |
167 | |
d205a7bf |
168 | return( date_intl( _("D, F j, Y g:i a"), $stamp ) ); |
d59837cf |
169 | |
0f1835f3 |
170 | } |
171 | |
d205a7bf |
172 | function getDateString( $stamp ) { |
d59837cf |
173 | |
2ed6f907 |
174 | global $invert_time; |
d205a7bf |
175 | |
d68a3926 |
176 | $now = time(); |
d205a7bf |
177 | |
178 | $dateZ = date('Z', $now ); |
d47b2518 |
179 | if ($invert_time) |
180 | $dateZ = - $dateZ; |
c57a3b1a |
181 | $midnight = $now - ($now % 86400) - $dateZ; |
d68a3926 |
182 | |
183 | if ($midnight < $stamp) { |
184 | // Today |
d59837cf |
185 | $date_format = _("g:i a"); |
186 | } else if ($midnight - 518400 < $stamp) { |
d68a3926 |
187 | // This week |
d59837cf |
188 | $date_format = _("D, g:i a"); |
d68a3926 |
189 | } else { |
190 | // before this week |
d59837cf |
191 | $date_format = _("M j, Y"); |
d68a3926 |
192 | } |
d59837cf |
193 | |
d205a7bf |
194 | return( date_intl( $date_format, $stamp ) ); |
195 | // return( date( $date_i, $stamp ) ); |
0f1835f3 |
196 | } |
197 | |
198 | function getTimeStamp($dateParts) { |
3302d0d4 |
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 | |
d068c0ec |
212 | // Simply check to see if the first element in the dateParts |
213 | // array is an integer or not. |
5e90d34a |
214 | // Since the day of week is optional, this check is needed. |
215 | // |
b9bfd165 |
216 | // The old code used eregi('mon|tue|wed|thu|fri|sat|sun', |
d068c0ec |
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. |
5e90d34a |
221 | // |
d068c0ec |
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. |
5e90d34a |
225 | // |
af354e93 |
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] = ''; |
5e90d34a |
231 | if (intval(trim($dateParts[0])) > 0) { |
b9bfd165 |
232 | $string = $dateParts[0] . ' ' . $dateParts[1] . ' ' . |
233 | $dateParts[2] . ' ' . $dateParts[3]; |
fde32e3f |
234 | return getGMTSeconds(strtotime($string), $dateParts[4]); |
5b10f02a |
235 | } |
b9bfd165 |
236 | $string = $dateParts[0] . ' ' . $dateParts[1] . ' ' . |
237 | $dateParts[2] . ' ' . $dateParts[3] . ' ' . $dateParts[4]; |
ca5b5e00 |
238 | if (isset($dateParts[5])) |
239 | return getGMTSeconds(strtotime($string), $dateParts[5]); |
240 | else |
241 | return getGMTSeconds(strtotime($string), ''); |
5e90d34a |
242 | } |
243 | |
d068c0ec |
244 | // I use this function for profiling. Should never be called in |
245 | // actual versions of squirrelmail released to public. |
d205a7bf |
246 | /* |
5e90d34a |
247 | function getmicrotime() { |
248 | $mtime = microtime(); |
b9bfd165 |
249 | $mtime = explode(' ',$mtime); |
5e90d34a |
250 | $mtime = $mtime[1] + $mtime[0]; |
251 | return ($mtime); |
9774bdef |
252 | } |
d205a7bf |
253 | */ |
9fd23330 |
254 | ?> |