Made things ready for saving messages to a sent folder.
[squirrelmail.git] / functions / date.php
... / ...
CommitLineData
1<?
2 /**
3 ** date.php
4 **
5 ** Takes a date and parses it into a usable format. The form that a
6 ** date SHOULD arrive in is:
7 ** <Tue,> 29 Jun 1999 09:52:11 -0500 (EDT)
8 ** (as specified in RFC 822) -- "Tue" is optional
9 **
10 **/
11
12 $date_php = true;
13
14 function getMinutes($hour) {
15 $date = $hour;
16
17 if (($hour == 0) || ($hour == "00"))
18 $date = "00";
19 else if (($hour == 1) || ($hour == "01"))
20 $date = "01";
21 else if (($hour == 2) || ($hour == "02"))
22 $date = "02";
23 else if (($hour == 3) || ($hour == "03"))
24 $date = "03";
25 else if (($hour == 4) || ($hour == "04"))
26 $date = "04";
27 else if (($hour == 5) || ($hour == "05"))
28 $date = "05";
29 else if (($hour == 6) || ($hour == "06"))
30 $date = "06";
31 else if (($hour == 7) || ($hour == "07"))
32 $date = "07";
33 else if (($hour == 8) || ($hour == "08"))
34 $date = "08";
35 else if (($hour == 9) || ($hour == "09"))
36 $date = "09";
37
38 return $date;
39 }
40
41 // corrects a time stamp to be the local time
42 function getGMTSeconds($stamp, $gmt) {
43 if (($gmt == "Pacific") || ($gmt == "PST") || ($gmt == "PDT"))
44 $gmt = "-0800";
45 if (($gmt == "Eastern") || ($gmt == "EST") || ($gmt == "EDT"))
46 $gmt = "-0500";
47 if (($gmt == "Central") || ($gmt == "CST") || ($gmt == "CDT"))
48 $gmt = "-0600";
49 if (($gmt == "Mountain") || ($gmt == "MST") || ($gmt == "MDT"))
50 $gmt = "-0700";
51
52 if (substr($gmt, 0, 1) == "-") {
53 $neg = true;
54 $gmt = substr($gmt, 1, strlen($gmt));
55 } else if (substr($gmt, 0, 1) == "+") {
56 $neg = false;
57 $gmt = substr($gmt, 1, strlen($gmt));
58 } else
59 $neg = false;
60
61 $gmt = substr($gmt, 0, 2);
62 $gmt = $gmt * 3600;
63 if ($neg == true)
64 $gmt = "-$gmt";
65 else
66 $gmt = "+$gmt";
67
68 /** now find what the server is at **/
69 $current = date("Z", time());
70
71 $stamp = (int)$stamp - (int)$gmt + (int)$current;
72
73 return $stamp;
74 }
75
76 function getHour($hour) {
77 $time = explode(":", $hour);
78 return $time[0];
79 }
80
81 function getMinute($min) {
82 $time = explode(":", $min);
83 return $time[1];
84 }
85
86 function getSecond($sec) {
87 $time = explode(":", $sec);
88 return $time[2];
89 }
90
91 function getMonthNum($month) {
92 if (eregi("jan|january", $month, $tmp))
93 $date = "01";
94 else if (eregi("feb|february|febuary", $month, $tmp))
95 $date = "02";
96 else if (eregi("mar|march", $month, $tmp))
97 $date = "03";
98 else if (eregi("apr|april", $month, $tmp))
99 $date = "04";
100 else if (eregi("may", $month, $tmp))
101 $date = "05";
102 else if (eregi("jun|june", $month, $tmp))
103 $date = "06";
104 else if (eregi("jul|july", $month, $tmp))
105 $date = "07";
106 else if (eregi("aug|august", $month, $tmp))
107 $date = "08";
108 else if (eregi("sep|sept|september", $month, $tmp))
109 $date = "09";
110 else if (eregi("oct|october", $month, $tmp))
111 $date = "10";
112 else if (eregi("nov|november", $month, $tmp))
113 $date = "11";
114 else if (eregi("dec|december", $month, $tmp))
115 $date = "12";
116
117 return $date;
118 }
119
120 function getDayOfWeek($day) {
121 $date = "{WEEKDAY}";
122
123 if (eregi("(mon|monday)", $day, $tmp))
124 $date = "Mon";
125 else if (eregi("(tue|tuesday)", $day, $tmp))
126 $date = "Tue";
127 else if (eregi("(wed|wednesday)", $day, $tmp))
128 $date = "Wed";
129 else if (eregi("(thurs|thu|thursday)", $day, $tmp))
130 $date = "Thu";
131 else if (eregi("(fri|friday)", $day, $tmp))
132 $date = "Fri";
133 else if (eregi("(sat|saturday)", $day, $tmp))
134 $date = "Sat";
135 else if (eregi("(sun|sunday)", $day, $tmp))
136 $date = "Sun";
137
138 return $date;
139 }
140
141 function getDayOfMonth($day) {
142 return ereg_replace("^0", "", $day); /* remove a preceeding 0 */
143 }
144
145 function getMonth($month) {
146 $date = "{MONTH}";
147 if (eregi("jan|january", $month, $tmp))
148 $date = "Jan";
149 else if (eregi("feb|february|febuary", $month, $tmp))
150 $date = "Feb";
151 else if (eregi("mar|march", $month, $tmp))
152 $date = "Mar";
153 else if (eregi("apr|april", $month, $tmp))
154 $date = "Apr";
155 else if (eregi("may", $month, $tmp))
156 $date = "May";
157 else if (eregi("jun|june", $month, $tmp))
158 $date = "Jun";
159 else if (eregi("jul|july", $month, $tmp))
160 $date = "Jul";
161 else if (eregi("aug|august", $month, $tmp))
162 $date = "Aug";
163 else if (eregi("sep|sept|september", $month, $tmp))
164 $date = "Sep";
165 else if (eregi("oct|october", $month, $tmp))
166 $date = "Oct";
167 else if (eregi("nov|november", $month, $tmp))
168 $date = "Nov";
169 else if (eregi("dec|december", $month, $tmp))
170 $date = "Dec";
171
172 return $date;
173 }
174
175 function getYear($year) {
176 return $year;
177 }
178
179 function getLongDateString($stamp) {
180 return date("D, F j, Y g:i a", $stamp);
181 }
182
183 function getDateString($stamp) {
184 return date("M j, Y", $stamp);
185 }
186
187 function getTimeStamp($dateParts) {
188 /** $dateParts[0] == <day of week> Mon, Tue, Wed
189 ** $dateParts[1] == <day of month> 23
190 ** $dateParts[2] == <month> Jan, Feb, Mar
191 ** $dateParts[3] == <year> 1999
192 ** $dateParts[4] == <time> 18:54:23 (HH:MM:SS)
193 ** $dateParts[5] == <from GMT> +0100
194 ** $dateParts[6] == <zone> (EDT)
195 **
196 ** NOTE: In RFC 822, it states that <day of week> is optional.
197 ** In that case, dateParts[0] would be the <day of month>
198 ** and everything would be bumped up one.
199 **/
200
201 // Simply check to see if the first element in the dateParts
202 // array is an integer or not.
203 // Since the day of week is optional, this check is needed.
204 //
205 // The old code used eregi("mon|tue|wed|thu|fri|sat|sun",
206 // $dateParts[0], $tmp) to find if the first element was the
207 // day of week or day of month. This is an expensive call
208 // (processing time) to have inside a loop. Doing it this way
209 // saves quite a bit of time for large mailboxes.
210 //
211 // It is also quicker to call explode only once rather than
212 // the 3 times it was getting called by calling the functions
213 // getHour, getMinute, and getSecond.
214 //
215 if (intval(trim($dateParts[0])) > 0) {
216 $time = explode(":", $dateParts[3]);
217 $d[0] = $time[0];
218 $d[1] = $time[1];
219 $d[2] = $time[2];
220 $d[3] = getMonthNum(trim($dateParts[1]));
221 $d[4] = getDayOfMonth(trim($dateParts[0]));
222 $d[5] = getYear(trim($dateParts[2]));
223 return getGMTSeconds(mktime($d[0], $d[1], $d[2], $d[3], $d[4], $d[5]), $dateParts[4]);
224 }
225 $time = explode(":", $dateParts[4]);
226 $d[0] = $time[0];
227 $d[1] = $time[1];
228 $d[2] = $time[2];
229 $d[3] = getMonthNum(trim($dateParts[2]));
230 $d[4] = getDayOfMonth(trim($dateParts[1]));
231 $d[5] = getYear(trim($dateParts[3]));
232 return getGMTSeconds(mktime($d[0], $d[1], $d[2], $d[3], $d[4], $d[5]), $dateParts[5]);
233 }
234
235 // I use this function for profiling. Should never be called in
236 // actual versions of squirrelmail released to public.
237 function getmicrotime() {
238 $mtime = microtime();
239 $mtime = explode(" ",$mtime);
240 $mtime = $mtime[1] + $mtime[0];
241 return ($mtime);
242 }
243?>