6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
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
17 require_once( '../functions/constants.php' );
19 // corrects a time stamp to be the local time
20 function getGMTSeconds($stamp, $gmt) {
22 if (($gmt == 'Pacific') ||
($gmt == 'PST')) {
24 } else if (($gmt == 'EDT')) {
26 } else if (($gmt == 'Eastern') ||
($gmt == 'EST') ||
($gmt == 'CDT')) {
28 } else if (($gmt == 'Central') ||
($gmt == 'CST') ||
($gmt == 'MDT')) {
30 } else if (($gmt == 'Mountain') ||
($gmt == 'MST') ||
($gmt == 'PDT')) {
32 } else if ($gmt == 'BST') {
34 } else if ($gmt == 'EET') {
36 } else if ($gmt == 'GMT') {
38 } else if ($gmt == 'HKT') {
40 } else if ($gmt == 'IST') {
42 } else if ($gmt == 'JST') {
44 } else if ($gmt == 'KST') {
46 } else if ($gmt == 'MET') {
48 } else if ($gmt == 'MET DST' ||
$gmt == 'METDST') {
52 if (substr($gmt, 0, 1) == '-') {
54 $gmt = substr($gmt, 1, strlen($gmt));
55 } else if (substr($gmt, 0, 1) == '+') {
57 $gmt = substr($gmt, 1, strlen($gmt));
62 $gmt = substr($gmt, 0, 2);
70 /** now find what the server is at **/
71 $current = date('Z', time());
73 $current = - $current;
75 $stamp = (int)$stamp - (int)$gmt +
(int)$current;
81 Switch system has been intentionaly choosed for the
82 internationalization of month and day names. The reason
83 is to make sure that _("") strings will go into the
87 function getDayName( $day_number ) {
89 switch( $day_number ) {
100 $ret = _("Wednesday");
103 $ret = _("Thursday");
109 $ret = _("Saturday");
117 function getMonthName( $month_number ) {
118 switch( $month_number ) {
123 $ret = _("February");
144 $ret = _("September");
150 $ret = _("November");
153 $ret = _("December");
161 function date_intl( $date_format, $stamp ) {
163 $ret = str_replace( 'D', '$1', $date_format );
164 $ret = str_replace( 'F', '$2', $ret );
165 $ret = str_replace( 'l', '$4', $ret );
166 $ret = str_replace( 'M', '$5', $ret );
167 $ret = date( '$3'. $ret . '$3', $stamp ); // Workaround for a PHP 4.0.4 problem
168 $ret = str_replace( '$1', substr( getDayName( date( 'w', $stamp ) ), 0, 3 ), $ret );
169 $ret = str_replace( '$5', substr( getMonthName( date( 'm', $stamp ) ), 0, 3 ), $ret );
170 $ret = str_replace( '$2', getMonthName( date( 'm', $stamp ) ), $ret );
171 $ret = str_replace( '$4', getDayName( date( 'w', $stamp ) ), $ret );
172 $ret = str_replace( '$3', '', $ret );
177 function getLongDateString( $stamp ) {
181 if ( $hour_format == SMPREF_TIME_12HR
) {
182 $date_format = _("D, F j, Y g:i a");
184 $date_format = _("D, F j, Y G:i");
187 return( date_intl( $date_format, $stamp ) );
191 function getDateString( $stamp ) {
193 global $invert_time, $hour_format;
197 $dateZ = date('Z', $now );
201 $midnight = $now - ($now %
86400) - $dateZ;
203 if ($midnight < $stamp) {
205 if ( $hour_format == SMPREF_TIME_12HR
) {
206 $date_format = _("g:i a");
208 $date_format = _("G:i");
210 } else if ($midnight - 518400 < $stamp) {
212 if ( $hour_format == SMPREF_TIME_12HR
) {
213 $date_format = _("D, g:i a");
215 $date_format = _("D, G:i");
218 /* before this week */
219 $date_format = _("M j, Y");
222 return( date_intl( $date_format, $stamp ) );
225 function getTimeStamp($dateParts) {
226 /** $dateParts[0] == <day of week> Mon, Tue, Wed
227 ** $dateParts[1] == <day of month> 23
228 ** $dateParts[2] == <month> Jan, Feb, Mar
229 ** $dateParts[3] == <year> 1999
230 ** $dateParts[4] == <time> 18:54:23 (HH:MM:SS)
231 ** $dateParts[5] == <from GMT> +0100
232 ** $dateParts[6] == <zone> (EDT)
234 ** NOTE: In RFC 822, it states that <day of week> is optional.
235 ** In that case, dateParts[0] would be the <day of month>
236 ** and everything would be bumped up one.
240 * Simply check to see if the first element in the dateParts
241 * array is an integer or not.
242 * Since the day of week is optional, this check is needed.
244 * The old code used eregi('mon|tue|wed|thu|fri|sat|sun',
245 * $dateParts[0], $tmp) to find if the first element was the
246 * day of week or day of month. This is an expensive call
247 * (processing time) to have inside a loop. Doing it this way
248 * saves quite a bit of time for large mailboxes.
250 * It is also quicker to call explode only once rather than
251 * the 3 times it was getting called by calling the functions
252 * getHour, getMinute, and getSecond.
255 if (! isset($dateParts[1])) {
258 if (! isset($dateParts[2])) {
261 if (! isset($dateParts[3])) {
264 if (! isset($dateParts[4])) {
267 if (! isset($dateParts[5])) {
270 if (intval(trim($dateParts[0])) > 0) {
271 $string = $dateParts[0] . ' ' . $dateParts[1] . ' ' .
272 $dateParts[2] . ' ' . $dateParts[3];
273 return getGMTSeconds(strtotime($string), $dateParts[4]);
275 $string = $dateParts[0] . ' ' . $dateParts[1] . ' ' .
276 $dateParts[2] . ' ' . $dateParts[3] . ' ' . $dateParts[4];
277 if (isset($dateParts[5])) {
278 return getGMTSeconds(strtotime($string), $dateParts[5]);
280 return getGMTSeconds(strtotime($string), '');
284 /* I use this function for profiling. Should never be called in
285 actual versions of squirrelmail released to public. */
287 function getmicrotime() {
288 $mtime = microtime();
289 $mtime = explode(' ',$mtime);
290 $mtime = $mtime[1] + $mtime[0];