* Made everything here global too to work with new include system
[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 ** $Id$
11 **/
12
13 if (defined ('date_php'))
14 return;
15 define ('date_php', true);
16
17 // corrects a time stamp to be the local time
18 function getGMTSeconds($stamp, $gmt) {
19 global $invert_time;
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';
42 else if ($gmt == 'KST')
43 $gmt = "+0900";
44 else if ($gmt == 'MET')
45 $gmt = '+0100';
46 else if ($gmt == 'MET DST' || $gmt == 'METDST')
47 $gmt = '+0200';
48
49 if (substr($gmt, 0, 1) == '-') {
50 $neg = true;
51 $gmt = substr($gmt, 1, strlen($gmt));
52 } else if (substr($gmt, 0, 1) == '+') {
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 **/
66 $current = date('Z', time());
67 if ($invert_time)
68 $current = - $current;
69 $stamp = (int)$stamp - (int)$gmt + (int)$current;
70
71 return $stamp;
72 }
73
74 function getLongDateString($stamp) {
75 return date('D, F j, Y g:i a', $stamp);
76 }
77
78 function getDateString($stamp) {
79 global $invert_time;
80 $now = time();
81 $dateZ = date('Z', $now);
82 if ($invert_time)
83 $dateZ = - $dateZ;
84 $midnight = $now - ($now % 86400) - $dateZ;
85
86 if ($midnight < $stamp) {
87 // Today
88 return date('g:i a', $stamp);
89 } else if ($midnight - (60 * 60 * 24 * 6) < $stamp) {
90 // This week
91 return date('D, g:i a', $stamp);
92 } else {
93 // before this week
94 return date('M j, Y', $stamp);
95 }
96 }
97
98 function getTimeStamp($dateParts) {
99 /** $dateParts[0] == <day of week> Mon, Tue, Wed
100 ** $dateParts[1] == <day of month> 23
101 ** $dateParts[2] == <month> Jan, Feb, Mar
102 ** $dateParts[3] == <year> 1999
103 ** $dateParts[4] == <time> 18:54:23 (HH:MM:SS)
104 ** $dateParts[5] == <from GMT> +0100
105 ** $dateParts[6] == <zone> (EDT)
106 **
107 ** NOTE: In RFC 822, it states that <day of week> is optional.
108 ** In that case, dateParts[0] would be the <day of month>
109 ** and everything would be bumped up one.
110 **/
111
112 // Simply check to see if the first element in the dateParts
113 // array is an integer or not.
114 // Since the day of week is optional, this check is needed.
115 //
116 // The old code used eregi('mon|tue|wed|thu|fri|sat|sun',
117 // $dateParts[0], $tmp) to find if the first element was the
118 // day of week or day of month. This is an expensive call
119 // (processing time) to have inside a loop. Doing it this way
120 // saves quite a bit of time for large mailboxes.
121 //
122 // It is also quicker to call explode only once rather than
123 // the 3 times it was getting called by calling the functions
124 // getHour, getMinute, and getSecond.
125 //
126 if (intval(trim($dateParts[0])) > 0) {
127 $string = $dateParts[0] . ' ' . $dateParts[1] . ' ' .
128 $dateParts[2] . ' ' . $dateParts[3];
129 return getGMTSeconds(strtotime($string), $dateParts[4]);
130 }
131 $string = $dateParts[0] . ' ' . $dateParts[1] . ' ' .
132 $dateParts[2] . ' ' . $dateParts[3] . ' ' . $dateParts[4];
133 if (isset($dateParts[5]))
134 return getGMTSeconds(strtotime($string), $dateParts[5]);
135 else
136 return getGMTSeconds(strtotime($string), '');
137 }
138
139 // I use this function for profiling. Should never be called in
140 // actual versions of squirrelmail released to public.
141 function getmicrotime() {
142 $mtime = microtime();
143 $mtime = explode(' ',$mtime);
144 $mtime = $mtime[1] + $mtime[0];
145 return ($mtime);
146 }
147 ?>