1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: namespace Kotchasan;
12:
13: 14: 15: 16: 17: 18: 19:
20: class Date
21: {
22: 23: 24:
25: private static $lang;
26:
27: 28: 29:
30: public function __construct()
31: {
32: self::$lang = Language::getItems(array(
33: 'DATE_SHORT',
34: 'DATE_LONG',
35: 'MONTH_SHORT',
36: 'MONTH_LONG',
37: 'YEAR_OFFSET',
38: ));
39: }
40:
41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52:
53: public static function compare($begin_date, $end_date)
54: {
55: if (is_string($begin_date) && preg_match('/([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2})(\s([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2}))?/', $begin_date, $match)) {
56: $begin_date = mktime(0, 0, 0, (int) $match[2], (int) $match[3], (int) $match[1]);
57: }
58: if (is_string($end_date) && preg_match('/([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2})(\s([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2}))?/', $end_date, $match)) {
59: $end_date = mktime(0, 0, 0, (int) $match[2], (int) $match[3], (int) $match[1]);
60: }
61: if ($end_date == $begin_date) {
62:
63: return array(
64: 'days' => 0,
65: 'year' => 0,
66: 'month' => 0,
67: 'day' => 0,
68: );
69: } else {
70:
71: $days = floor(($end_date - $begin_date) / 86400);
72: if ($end_date < $begin_date) {
73: $tmp = $begin_date;
74: $begin_date = $end_date;
75: $end_date = $tmp;
76: }
77: }
78: $Year1 = (int) date('Y', $begin_date);
79: $Month1 = (int) date('m', $begin_date);
80: $Day1 = (int) date('d', $begin_date);
81: $Year2 = (int) date('Y', $end_date);
82: $Month2 = (int) date('m', $end_date);
83: $Day2 = (int) date('d', $end_date);
84:
85: $months = array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
86:
87: if (($Year2 % 4) == 0) {
88: $months[2] = 29;
89: }
90:
91: if ((($Year2 % 100) == 0) & (($Year2 % 400) != 0)) {
92: $months[2] = 28;
93: }
94: if (abs($days) < $months[$Month1]) {
95:
96: return array(
97: 'days' => $days,
98: 'year' => 0,
99: 'month' => 0,
100: 'day' => abs($days),
101: );
102: } else {
103:
104: $YearDiff = $Year2 - $Year1;
105: if ($Month2 >= $Month1) {
106: $MonthDiff = $Month2 - $Month1;
107: } else {
108: --$YearDiff;
109: $MonthDiff = 12 + $Month2 - $Month1;
110: }
111: if ($Day1 > $months[$Month2]) {
112: $Day1 = 0;
113: } elseif ($Day1 > $Day2) {
114: $Month2 = $Month2 == 1 ? 13 : $Month2;
115: $Day2 += $months[$Month2 - 1];
116: --$MonthDiff;
117: }
118: return array(
119: 'days' => $days,
120: 'year' => $YearDiff,
121: 'month' => $MonthDiff,
122: 'day' => $Day2 - $Day1,
123: );
124: }
125: }
126:
127: 128: 129: 130: 131: 132: 133: 134: 135: 136:
137: public static function timeDiff($firstTime, $lastTime)
138: {
139: $firstTime = strtotime($firstTime);
140: $lastTime = strtotime($lastTime);
141: $timeDiff = $lastTime - $firstTime;
142: return $timeDiff;
143: }
144:
145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156:
157: public static function dateName($date, $short_date = true)
158: {
159:
160: if (!isset(self::$lang)) {
161: new static();
162: }
163: $var = $short_date ? self::$lang['DATE_SHORT'] : self::$lang['DATE_LONG'];
164: return isset($var[$date]) ? $var[$date] : '';
165: }
166:
167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179:
180: public static function format($time = 0, $format = null)
181: {
182: if ($time === 0) {
183: $time = time();
184: } elseif (is_string($time)) {
185: if (preg_match('/^[0-9]+$/', $time)) {
186: $time = (int) $time;
187: } else {
188: $time = strtotime($time);
189: }
190: } elseif (!is_int($time)) {
191: return '';
192: }
193:
194: if (!isset(self::$lang)) {
195: new static();
196: }
197: $format = empty($format) ? 'DATE_FORMAT' : $format;
198: $format = Language::get($format);
199: if (preg_match_all('/(.)/u', $format, $match)) {
200: $ret = '';
201: foreach ($match[0] as $item) {
202: switch ($item) {
203: case ' ':
204: case ':':
205: case '/':
206: case '-':
207: case '.':
208: case ',':
209: $ret .= $item;
210: break;
211: case 'l':
212: $ret .= self::$lang['DATE_SHORT'][date('w', $time)];
213: break;
214: case 'L':
215: $ret .= self::$lang['DATE_LONG'][date('w', $time)];
216: break;
217: case 'M':
218: $ret .= self::$lang['MONTH_SHORT'][date('n', $time)];
219: break;
220: case 'F':
221: $ret .= self::$lang['MONTH_LONG'][date('n', $time)];
222: break;
223: case 'Y':
224: $ret .= (int) date('Y', $time) + (int) self::$lang['YEAR_OFFSET'];
225: break;
226: default:
227: $ret .= trim($item) == '' ? ' ' : date($item, $time);
228: break;
229: }
230: }
231: } else {
232: $ret = date($format, $time);
233: }
234: return $ret;
235: }
236:
237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248:
249: public static function monthName($month, $short_month = true)
250: {
251:
252: if (!isset(self::$lang)) {
253: new static();
254: }
255: $var = $short_month ? self::$lang['MONTH_SHORT'] : self::$lang['MONTH_LONG'];
256: return isset($var[$month]) ? $var[$month] : '';
257: }
258:
259: 260: 261: 262: 263: 264: 265: 266:
267: public static function parse($date)
268: {
269: if (preg_match('/([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2})(\s([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2}))?/', $date, $match)) {
270: if (isset($match[4])) {
271: return array('y' => $match[1], 'm' => $match[2], 'd' => $match[3], 'h' => $match[5], 'i' => $match[6], 's' => $match[7]);
272: } else {
273: return array('y' => $match[1], 'm' => $match[2], 'd' => $match[3]);
274: }
275: }
276: return false;
277: }
278: }
279: