4760d0290a3b0b40d579bc257fa4b2e4057a1a0a
[squirrelmail.git] / functions / date.php
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 function getHour($hour) {
13 $time = explode(":", $hour);
14 return $time[0];
15 }
16
17 function getMinute($min) {
18 $time = explode(":", $min);
19 return $time[1];
20 }
21
22 function getSecond($sec) {
23 $time = explode(":", $sec);
24 return $time[2];
25 }
26
27 function getMonthNum($month) {
28 if (eregi("jan|january", $month, $tmp))
29 $date = "01";
30 else if (eregi("feb|february|febuary", $month, $tmp))
31 $date = "02";
32 else if (eregi("mar|march", $month, $tmp))
33 $date = "03";
34 else if (eregi("apr|april", $month, $tmp))
35 $date = "04";
36 else if (eregi("may", $month, $tmp))
37 $date = "05";
38 else if (eregi("jun|june", $month, $tmp))
39 $date = "06";
40 else if (eregi("jul|july", $month, $tmp))
41 $date = "07";
42 else if (eregi("aug|august", $month, $tmp))
43 $date = "08";
44 else if (eregi("sep|sept|september", $month, $tmp))
45 $date = "09";
46 else if (eregi("oct|october", $month, $tmp))
47 $date = "10";
48 else if (eregi("nov|november", $month, $tmp))
49 $date = "11";
50 else if (eregi("dec|december", $month, $tmp))
51 $date = "12";
52
53 return $date;
54 }
55
56 function getDayOfWeek($day) {
57 $date = "{WEEKDAY}";
58
59 if (eregi("(mon|monday)", $day, $tmp))
60 $date = "Mon";
61 else if (eregi("(tue|tuesday)", $day, $tmp))
62 $date = "Tue";
63 else if (eregi("(wed|wednesday)", $day, $tmp))
64 $date = "Wed";
65 else if (eregi("(thurs|thu|thursday)", $day, $tmp))
66 $date = "Thu";
67 else if (eregi("(fri|friday)", $day, $tmp))
68 $date = "Fri";
69 else if (eregi("(sat|saturday)", $day, $tmp))
70 $date = "Sat";
71 else if (eregi("(sun|sunday)", $day, $tmp))
72 $date = "Sun";
73
74 return $date;
75 }
76
77 function getDayOfMonth($day) {
78 return ereg_replace("^0", "", $day); /* remove a preceeding 0 */
79 }
80
81 function getMonth($month) {
82 $date = "{MONTH}";
83 if (eregi("jan|january", $month, $tmp))
84 $date = "Jan";
85 else if (eregi("feb|february|febuary", $month, $tmp))
86 $date = "Feb";
87 else if (eregi("mar|march", $month, $tmp))
88 $date = "Mar";
89 else if (eregi("apr|april", $month, $tmp))
90 $date = "Apr";
91 else if (eregi("may", $month, $tmp))
92 $date = "May";
93 else if (eregi("jun|june", $month, $tmp))
94 $date = "Jun";
95 else if (eregi("jul|july", $month, $tmp))
96 $date = "Jul";
97 else if (eregi("aug|august", $month, $tmp))
98 $date = "Aug";
99 else if (eregi("sep|sept|september", $month, $tmp))
100 $date = "Sep";
101 else if (eregi("oct|october", $month, $tmp))
102 $date = "Oct";
103 else if (eregi("nov|november", $month, $tmp))
104 $date = "Nov";
105 else if (eregi("dec|december", $month, $tmp))
106 $date = "Dec";
107
108 return $date;
109 }
110
111 function getYear($year) {
112 return $year;
113 }
114
115 function getDateString($dateParts) {
116 /** $dateParts[0] == <day of week> Mon, Tue, Wed
117 ** $dateParts[1] == <day of month> 23
118 ** $dateParts[2] == <month> Jan, Feb, Mar
119 ** $dateParts[3] == <year> 1999
120 ** $dateParts[4] == <time> 18:54:23 (HH:MM:SS)
121 ** $dateParts[5] == <from GMT> +0100
122 ** $dateParts[6] == <zone> (EDT)
123 **
124 ** NOTE: In RFC 822, it states that <day of week> is optional.
125 ** In that case, dateParts[0] would be the <day of month>
126 ** and everything would be bumped up one.
127 **/
128
129 /* if the first part is a day */
130 if (eregi("mon|tue|wed|thu|fri|sat|sun", $dateParts[0], $tmp)) {
131 $dateParts[0] = getDayOfWeek($dateParts[0]);
132 $dateParts[1] = getDayOfMonth($dateParts[1]);
133 $dateParts[2] = getMonth($dateParts[2]);
134 $dateParts[3] = getYear($dateParts[3]);
135 return "$dateParts[2] $dateParts[1], $dateParts[3]";
136 }
137 $dateParts[0] = getDayOfMonth($dateParts[0]);
138 $dateParts[1] = getMonth($dateParts[1]);
139 $dateParts[2] = getYear($dateParts[2]);
140 return "$dateParts[1] $dateParts[0], $dateParts[2]";
141 }
142
143 function getTimeStamp($dateParts) {
144 if (eregi("mon|tue|wed|thu|fri|sat|sun", $dateParts[0], $tmp)) {
145 $d[0] = getHour($dateParts[4]);
146 $d[1] = getMinute($dateParts[4]);
147 $d[2] = getSecond($dateParts[4]);
148 $d[3] = getMonthNum($dateParts[2]);
149 $d[4] = getDayOfMonth($dateParts[1]);
150 $d[5] = getYear($dateParts[3]);
151 return mktime($d[0], $d[1], $d[2], $d[3], $d[4], $d[5]);
152 }
153 $d[0] = getHour($dateParts[3]);
154 $d[1] = getMinute($dateParts[3]);
155 $d[2] = getSecond($dateParts[3]);
156 $d[3] = getMonthNum($dateParts[1]);
157 $d[4] = getDayOfMonth($dateParts[0]);
158 $d[5] = getYear($dateParts[2]);
159 return mktime($d[0], $d[1], $d[2], $d[3], $d[4], $d[5]);
160 }
161 ?>