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