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(SM_PATH
. 'functions/constants.php');
19 /* corrects a time stamp to be the local time */
20 function getGMTSeconds($stamp, $gmt) {
23 /* date couldn't be parsed */
73 if (substr($gmt, 0, 1) == '-') {
75 $gmt = substr($gmt, 1, strlen($gmt));
76 } else if (substr($gmt, 0, 1) == '+') {
78 $gmt = substr($gmt, 1, strlen($gmt));
83 $difference = substr($gmt, 2, 2);
84 $gmt = substr($gmt, 0, 2);
85 $gmt = ($gmt +
($difference / 60)) * 3600;
92 /** now find what the server is at **/
93 $current = date('Z', time());
95 $current = - $current;
97 $stamp = (int)$stamp - (int)$gmt +
(int)$current;
103 Switch system has been intentionaly chosen for the
104 internationalization of month and day names. The reason
105 is to make sure that _("") strings will go into the
109 function getDayName( $day_number ) {
111 switch( $day_number ) {
122 $ret = _("Wednesday");
125 $ret = _("Thursday");
131 $ret = _("Saturday");
139 function getMonthName( $month_number ) {
140 switch( $month_number ) {
145 $ret = _("February");
166 $ret = _("September");
172 $ret = _("November");
175 $ret = _("December");
183 function date_intl( $date_format, $stamp ) {
185 $ret = str_replace( 'D', '$1', $date_format );
186 $ret = str_replace( 'F', '$2', $ret );
187 $ret = str_replace( 'l', '$4', $ret );
188 $ret = str_replace( 'M', '$5', $ret );
189 $ret = date( '$3'. $ret . '$3', $stamp ); // Workaround for a PHP 4.0.4 problem
190 $ret = str_replace( '$1', substr( getDayName( date( 'w', $stamp ) ), 0, 3 ), $ret );
191 $ret = str_replace( '$5', substr( getMonthName( date( 'm', $stamp ) ), 0, 3 ), $ret );
192 $ret = str_replace( '$2', getMonthName( date( 'm', $stamp ) ), $ret );
193 $ret = str_replace( '$4', getDayName( date( 'w', $stamp ) ), $ret );
194 $ret = str_replace( '$3', '', $ret );
199 function getLongDateString( $stamp ) {
207 if ( $hour_format == SMPREF_TIME_12HR
) {
208 $date_format = _("D, F j, Y g:i a");
210 $date_format = _("D, F j, Y G:i");
213 return( date_intl( $date_format, $stamp ) );
217 function getDateString( $stamp ) {
219 global $invert_time, $hour_format;
221 if ( $stamp == -1 ) {
227 $dateZ = date('Z', $now );
231 $midnight = $now - ($now %
86400) - $dateZ;
233 if ($midnight < $stamp) {
235 if ( $hour_format == SMPREF_TIME_12HR
) {
236 $date_format = _("g:i a");
238 $date_format = _("G:i");
240 } else if ($midnight - 518400 < $stamp) {
242 if ( $hour_format == SMPREF_TIME_12HR
) {
243 $date_format = _("D, g:i a");
245 $date_format = _("D, G:i");
248 /* before this week */
249 $date_format = _("M j, Y");
252 return( date_intl( $date_format, $stamp ) );
255 function getTimeStamp($dateParts) {
256 /** $dateParts[0] == <day of week> Mon, Tue, Wed
257 ** $dateParts[1] == <day of month> 23
258 ** $dateParts[2] == <month> Jan, Feb, Mar
259 ** $dateParts[3] == <year> 1999
260 ** $dateParts[4] == <time> 18:54:23 (HH:MM:SS)
261 ** $dateParts[5] == <from GMT> +0100
262 ** $dateParts[6] == <zone> (EDT)
264 ** NOTE: In RFC 822, it states that <day of week> is optional.
265 ** In that case, dateParts[0] would be the <day of month>
266 ** and everything would be bumped up one.
270 * Simply check to see if the first element in the dateParts
271 * array is an integer or not.
272 * Since the day of week is optional, this check is needed.
275 /* validate zone before we uses strtotime */
276 if (isset($dateParts[6]) && $dateParts[6]{0} != '(') {
277 $dateParts[6] = '('.$dateParts[6].')';
279 $string = implode (' ', $dateParts);
281 if (! isset($dateParts[4])) {
284 if (! isset($dateParts[5])) {
288 if (intval(trim($dateParts[0])) > 0) {
289 return getGMTSeconds(strtotime($string), $dateParts[4]);
291 return getGMTSeconds(strtotime($string), $dateParts[5]);
294 /* I use this function for profiling. Should never be called in
295 actual versions of squirrelmail released to public. */
297 function getmicrotime() {
298 $mtime = microtime();
299 $mtime = explode(' ',$mtime);
300 $mtime = $mtime[1] + $mtime[0];