- improved date parsing
[squirrelmail.git] / functions / date.php
1 <?php
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 // corrects a time stamp to be the local time
15 function getGMTSeconds($stamp, $gmt) {
16 if (($gmt == "Pacific") || ($gmt == "PST"))
17 $gmt = "-0800";
18 else if (($gmt == "EDT"))
19 $gmt = "-0400";
20 else if (($gmt == "Eastern") || ($gmt == "EST") || ($gmt == "CDT"))
21 $gmt = "-0500";
22 else if (($gmt == "Central") || ($gmt == "CST") || ($gmt == "MDT"))
23 $gmt = "-0600";
24 else if (($gmt == "Mountain") || ($gmt == "MST") || ($gmt == "PDT"))
25 $gmt = "-0700";
26 else if ($gmt == "BST")
27 $gmt = "+0100";
28 else if ($gmt == "EET")
29 $gmt = "+0200";
30 else if ($gmt == "GMT")
31 $gmt = "+0000";
32 else if ($gmt == "HKT")
33 $gmt = "+0800";
34 else if ($gmt == "IST")
35 $gmt = "+0200";
36 else if ($gmt == "JST")
37 $gmt = "+0900";
38 else if ($gmt == "MET")
39 $gmt = "+0100";
40 else if ($gmt == "MET DST" || $gmt == "METDST")
41 $gmt = "+0200";
42
43 if (substr($gmt, 0, 1) == "-") {
44 $neg = true;
45 $gmt = substr($gmt, 1, strlen($gmt));
46 } else if (substr($gmt, 0, 1) == "+") {
47 $neg = false;
48 $gmt = substr($gmt, 1, strlen($gmt));
49 } else
50 $neg = false;
51
52 $gmt = substr($gmt, 0, 2);
53 $gmt = $gmt * 3600;
54 if ($neg == true)
55 $gmt = "-$gmt";
56 else
57 $gmt = "+$gmt";
58
59 /** now find what the server is at **/
60 $current = date("Z", time());
61 $stamp = (int)$stamp - (int)$gmt + (int)$current;
62
63 return $stamp;
64 }
65
66 function getLongDateString($stamp) {
67 return date("D, F j, Y g:i a", $stamp);
68 }
69
70 function getDateString($stamp) {
71 $now = time();
72 $midnight = $now - ($now % 86400) - 86400 - date("Z", $now);
73
74 if ($midnight < $stamp) {
75 // Today
76 return date("g:i a", $stamp);
77 } else if ($midnight - (60 * 60 * 24 * 6) < $stamp) {
78 // This week
79 return date("D, g:i a", $stamp);
80 } else {
81 // before this week
82 return date("M j, Y", $stamp);
83 }
84 }
85
86 function getTimeStamp($dateParts) {
87 /** $dateParts[0] == <day of week> Mon, Tue, Wed
88 ** $dateParts[1] == <day of month> 23
89 ** $dateParts[2] == <month> Jan, Feb, Mar
90 ** $dateParts[3] == <year> 1999
91 ** $dateParts[4] == <time> 18:54:23 (HH:MM:SS)
92 ** $dateParts[5] == <from GMT> +0100
93 ** $dateParts[6] == <zone> (EDT)
94 **
95 ** NOTE: In RFC 822, it states that <day of week> is optional.
96 ** In that case, dateParts[0] would be the <day of month>
97 ** and everything would be bumped up one.
98 **/
99
100 // Simply check to see if the first element in the dateParts
101 // array is an integer or not.
102 // Since the day of week is optional, this check is needed.
103 //
104 // The old code used eregi("mon|tue|wed|thu|fri|sat|sun",
105 // $dateParts[0], $tmp) to find if the first element was the
106 // day of week or day of month. This is an expensive call
107 // (processing time) to have inside a loop. Doing it this way
108 // saves quite a bit of time for large mailboxes.
109 //
110 // It is also quicker to call explode only once rather than
111 // the 3 times it was getting called by calling the functions
112 // getHour, getMinute, and getSecond.
113 //
114 if (intval(trim($dateParts[0])) > 0) {
115 $string = $dateParts[0] . " " . $dateParts[1] . " " . $dateParts[2] . " " . $dateParts[3];
116 return getGMTSeconds(strtotime($string), $dateParts[4]);
117 }
118 $string = $dateParts[0] . " " . $dateParts[1] . " " . $dateParts[2] . " " . $dateParts[3] . " " . $dateParts[4];
119 return getGMTSeconds(strtotime($string), $dateParts[5]);
120 }
121
122 // I use this function for profiling. Should never be called in
123 // actual versions of squirrelmail released to public.
124 function getmicrotime() {
125 $mtime = microtime();
126 $mtime = explode(" ",$mtime);
127 $mtime = $mtime[1] + $mtime[0];
128 return ($mtime);
129 }
130 ?>