1: <?php
2: 3: 4: 5:
6:
7: 8: 9: 10: 11: 12: 13:
14: class Horde_Date_Utils
15: {
16: 17: 18: 19: 20: 21: 22:
23: public static function isLeapYear($year)
24: {
25: if (strlen($year) != 4 || preg_match('/\D/', $year)) {
26: return false;
27: }
28:
29: return (($year % 4 == 0 && $year % 100 != 0) || $year % 400 == 0);
30: }
31:
32: 33: 34: 35: 36: 37: 38: 39: 40:
41: public static function firstDayOfWeek($week, $year)
42: {
43: return new Horde_Date(sprintf('%04dW%02d', $year, $week));
44: }
45:
46: 47: 48: 49: 50: 51: 52: 53:
54: public static function daysInMonth($month, $year)
55: {
56: static $cache = array();
57: if (!isset($cache[$year][$month])) {
58: $date = new DateTime(sprintf('%04d-%02d-01', $year, $month));
59: $cache[$year][$month] = $date->format('t');
60: }
61: return $cache[$year][$month];
62: }
63:
64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77:
78: public static function relativeDateTime($time, $date_format = '%x',
79: $time_format = '%X')
80: {
81: $date = new Horde_Date($time);
82:
83: $delta = time() - $date->timestamp();
84: if ($delta < 60) {
85: return sprintf(Horde_Date_Translation::ngettext("%d second ago", "%d seconds ago", $delta), $delta);
86: }
87:
88: $delta = round($delta / 60);
89: if ($delta < 60) {
90: return sprintf(Horde_Date_Translation::ngettext("%d minute ago", "%d minutes ago", $delta), $delta);
91: }
92:
93: $delta = round($delta / 60);
94: if ($delta < 24) {
95: return sprintf(Horde_Date_Translation::ngettext("%d hour ago", "%d hours ago", $delta), $delta);
96: }
97:
98: if ($delta > 24 && $delta < 48) {
99: $date = new Horde_Date($time);
100: return sprintf(Horde_Date_Translation::t("yesterday at %s"), $date->strftime($time_format));
101: }
102:
103: $delta = round($delta / 24);
104: if ($delta < 7) {
105: return sprintf(Horde_Date_Translation::t("%d days ago"), $delta);
106: }
107:
108: if (round($delta / 7) < 5) {
109: $delta = round($delta / 7);
110: return sprintf(Horde_Date_Translation::ngettext("%d week ago", "%d weeks ago", $delta), $delta);
111: }
112:
113:
114: return $date->strftime($date_format);
115: }
116:
117: 118: 119: 120: 121: 122: 123: 124: 125:
126: public static function strftime2date($format)
127: {
128: $replace = array(
129: '/%a/' => 'D',
130: '/%A/' => 'l',
131: '/%d/' => 'd',
132: '/%e/' => 'j',
133: '/%j/' => 'z',
134: '/%u/' => 'N',
135: '/%w/' => 'w',
136: '/%U/' => '',
137: '/%V/' => 'W',
138: '/%W/' => '',
139: '/%b/' => 'M',
140: '/%B/' => 'F',
141: '/%h/' => 'M',
142: '/%m/' => 'm',
143: '/%C/' => '',
144: '/%g/' => '',
145: '/%G/' => 'o',
146: '/%y/' => 'y',
147: '/%Y/' => 'Y',
148: '/%H/' => 'H',
149: '/%I/' => 'h',
150: '/%i/' => 'g',
151: '/%M/' => 'i',
152: '/%p/' => 'A',
153: '/%P/' => 'a',
154: '/%r/' => 'h:i:s A',
155: '/%R/' => 'H:i',
156: '/%S/' => 's',
157: '/%T/' => 'H:i:s',
158: '/%X/e' => 'Horde_Date_Utils::strftime2date(Horde_Nls::getLangInfo(T_FMT))',
159: '/%z/' => 'O',
160: '/%Z/' => '',
161: '/%c/' => '',
162: '/%D/' => 'm/d/y',
163: '/%F/' => 'Y-m-d',
164: '/%s/' => 'U',
165: '/%x/e' => 'Horde_Date_Utils::strftime2date(Horde_Nls::getLangInfo(D_FMT))',
166: '/%n/' => "\n",
167: '/%t/' => "\t",
168: '/%%/' => '%'
169: );
170:
171: return preg_replace(array_keys($replace), array_values($replace), $format);
172: }
173:
174: }
175: