Fix: Internationalization of date strings.
[squirrelmail.git] / functions / date.php
CommitLineData
59177427 1<?php
3302d0d4 2 /**
a09387f4 3 ** date.php
3302d0d4 4 **
5 ** Takes a date and parses it into a usable format. The form that a
6 ** date SHOULD arrive in is:
5b10f02a 7 ** <Tue,> 29 Jun 1999 09:52:11 -0500 (EDT)
b9bfd165 8 ** (as specified in RFC 822) -- 'Tue' is optional
3302d0d4 9 **
245a6892 10 ** $Id$
3302d0d4 11 **/
12
f435778e 13 if (defined ('date_php'))
14 return;
15 define ('date_php', true);
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
d59837cf 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 **/
80
81 function getDayName( $day_number ) {
82
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 );
109 }
110
111 function getMonthName( $week_number ) {
112 switch( $week_number ) {
113 case 1:
114 $ret = _("January");
115 break;
116 case 2:
117 $ret = _("February");
118 break;
119 case 3:
120 $ret = _("March");
121 break;
122 case 4:
123 $ret = _("April");
124 break;
125 case 5:
126 $ret = _("May");
127 break;
128 case 6:
129 $ret = _("June");
130 break;
131 case 7:
132 $ret = _("July");
133 break;
134 case 8:
135 $ret = _("August");
136 break;
137 case 9:
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 }
154
155 function date_intl( $date_format ) {
156
157 $date_format = str_replace( 'D', '[D]', $date_format );
158 $date_format = str_replace( 'F', '[F]', $date_format );
159 $date_format = str_replace( '[D]',
160 ereg_replace( "([a-zA-Z])", "\\\\1", substr( getDayName( date( 'w', $stamp ) ), 0, 3 ) ),
161 $date_format );
162 $date_format = str_replace( '[F]',
163 ereg_replace( "([a-zA-Z])", "\\\\1", getMonthName( date( 'n', $stamp ) ) ),
164 $date_format );
165 return( $date_format . ' ' );
166 }
167
0f1835f3 168 function getLongDateString($stamp) {
d59837cf 169
170 $date_format = date_intl( _("D, F j, Y g:i a") );
171 return date( $date_format, $stamp );
172
0f1835f3 173 }
174
175 function getDateString($stamp) {
d59837cf 176
2ed6f907 177 global $invert_time;
d59837cf 178
d68a3926 179 $now = time();
b9bfd165 180 $dateZ = date('Z', $now);
d47b2518 181 if ($invert_time)
182 $dateZ = - $dateZ;
c57a3b1a 183 $midnight = $now - ($now % 86400) - $dateZ;
d68a3926 184
185 if ($midnight < $stamp) {
186 // Today
d59837cf 187 $date_format = _("g:i a");
188 } else if ($midnight - 518400 < $stamp) {
d68a3926 189 // This week
d59837cf 190 $date_format = _("D, g:i a");
d68a3926 191 } else {
192 // before this week
d59837cf 193 $date_format = _("M j, Y");
d68a3926 194 }
d59837cf 195
196 return( date( date_intl( $date_format ), $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];
283db905 239 if (isset($dateParts[5]))
240 return getGMTSeconds(strtotime($string), $dateParts[5]);
241 else
b9bfd165 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.
5e90d34a 247 function getmicrotime() {
248 $mtime = microtime();
b9bfd165 249 $mtime = explode(' ',$mtime);
5e90d34a 250 $mtime = $mtime[1] + $mtime[0];
251 return ($mtime);
9774bdef 252 }
3302d0d4 253?>