6 * Copyright (c) 1999-2003 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, $tzc) {
21 /* date couldn't be parsed */
25 /* timezone correction, expressed as `shhmm' */
73 if (substr($tzc, 0, 1) == '-') {
75 } else if (substr($tzc, 0, 1) != '+') {
78 $hh = substr($tzc,1,2);
79 $mm = substr($tzc,3,2);
80 $iTzc = ($hh * 60 +
$mm) * 60;
81 if ($neg) $iTzc = -1 * (int) $iTzc;
84 /** now find what the server is at **/
85 $current = date('Z', time());
86 /* stamp in local timezone */
93 Switch system has been intentionaly chosen for the
94 internationalization of month and day names. The reason
95 is to make sure that _("") strings will go into the
99 function getDayName( $day_number ) {
101 switch( $day_number ) {
112 $ret = _("Wednesday");
115 $ret = _("Thursday");
121 $ret = _("Saturday");
129 function getMonthName( $month_number ) {
130 switch( $month_number ) {
135 $ret = _("February");
156 $ret = _("September");
162 $ret = _("November");
165 $ret = _("December");
173 function date_intl( $date_format, $stamp ) {
175 $ret = str_replace( 'D', '$1', $date_format );
176 $ret = str_replace( 'F', '$2', $ret );
177 $ret = str_replace( 'l', '$4', $ret );
178 $ret = str_replace( 'M', '$5', $ret );
179 $ret = date( '$3'. $ret . '$3', $stamp ); // Workaround for a PHP 4.0.4 problem
180 $ret = str_replace( '$1', substr( getDayName( date( 'w', $stamp ) ), 0, 3 ), $ret );
181 $ret = str_replace( '$5', substr( getMonthName( date( 'm', $stamp ) ), 0, 3 ), $ret );
182 $ret = str_replace( '$2', getMonthName( date( 'm', $stamp ) ), $ret );
183 $ret = str_replace( '$4', getDayName( date( 'w', $stamp ) ), $ret );
184 $ret = str_replace( '$3', '', $ret );
189 function getLongDateString( $stamp ) {
197 if ( $hour_format == SMPREF_TIME_12HR
) {
198 $date_format = _("D, F j, Y g:i a");
200 $date_format = _("D, F j, Y G:i");
203 return( date_intl( $date_format, $stamp ) );
207 function getDateString( $stamp ) {
209 global $invert_time, $hour_format;
211 if ( $stamp == -1 ) {
217 $dateZ = date('Z', $now );
221 $midnight = $now - ($now %
86400) - $dateZ;
223 if ($midnight < $stamp) {
225 if ( $hour_format == SMPREF_TIME_12HR
) {
226 $date_format = _("g:i a");
228 $date_format = _("G:i");
230 } else if ($midnight - 518400 < $stamp) {
232 if ( $hour_format == SMPREF_TIME_12HR
) {
233 $date_format = _("D, g:i a");
235 $date_format = _("D, G:i");
238 /* before this week */
239 $date_format = _("M j, Y");
242 return( date_intl( $date_format, $stamp ) );
245 function getTimeStamp($dateParts) {
246 /** $dateParts[0] == <day of week> Mon, Tue, Wed
247 ** $dateParts[1] == <day of month> 23
248 ** $dateParts[2] == <month> Jan, Feb, Mar
249 ** $dateParts[3] == <year> 1999
250 ** $dateParts[4] == <time> 18:54:23 (HH:MM:SS)
251 ** $dateParts[5] == <from GMT> +0100
252 ** $dateParts[6] == <zone> (EDT)
254 ** NOTE: In RFC 822, it states that <day of week> is optional.
255 ** In that case, dateParts[0] would be the <day of month>
256 ** and everything would be bumped up one.
260 * Simply check to see if the first element in the dateParts
261 * array is an integer or not.
262 * Since the day of week is optional, this check is needed.
264 if (count($dateParts) <2) {
268 /* remove day of week */
269 if (!is_numeric(trim($dateParts[0]))) {
270 $dataParts = array_shift($dateParts);
272 /* calculate timestamp separated from the zone and obs-zone */
273 $stamp = strtotime(implode (' ', array_splice ($dateParts,0,4)));
274 if (!isset($dateParts[0])) {
275 $dateParts[0] = '+0000';
278 if (!preg_match('/^[+-]{1}[0-9]{4}$/',$dateParts[0])) {
279 /* zone in obs-zone format */
280 if (preg_match('/\((.+)\)/',$dateParts[0],$regs)) {
281 $obs_zone = $regs[1];
283 $obs_zone = $dateParts[0];
285 return getGMTSeconds($stamp, $obs_zone);
287 return getGMTSeconds($stamp, $dateParts[0]);
291 /* I use this function for profiling. Should never be called in
292 actual versions of squirrelmail released to public. */
294 function getmicrotime() {
295 $mtime = microtime();
296 $mtime = explode(' ',$mtime);
297 $mtime = $mtime[1] + $mtime[0];