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