1: <?php defined('_JOOS_CORE') or exit();
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16: class joosDateTime
17: {
18: 19: 20: 21: 22:
23: private static $date_translation = array('am' => 'дп', 'pm' => 'пп', 'AM' => 'ДП', 'PM' => 'ПП', 'Monday' => 'Понедельник', 'Mon' => 'Пн', 'Tuesday' => 'Вторник', 'Tue' => 'Вт', 'Wednesday' => 'Среда', 'Wed' => 'Ср', 'Thursday' => 'Четверг', 'Thu' => 'Чт', 'Friday' => 'Пятница', 'Fri' => 'Пт', 'Saturday' => 'Суббота', 'Sat' => 'Сб', 'Sunday' => 'Воскресенье', 'Sun' => 'Вс', 'January' => 'Января', 'Jan' => 'Янв', 'February' => 'Февраля', 'Feb' => 'Фев', 'March' => 'Марта', 'Mar' => 'Мар', 'April' => 'Апреля', 'Apr' => 'Апр', 'May' => 'Мая', 'June' => 'Июня', 'Jun' => 'Июн', 'July' => 'Июля', 'Jul' => 'Июл', 'August' => 'Августа', 'Aug' => 'Авг', 'September' => 'Сентября', 'Sep' => 'Сен', 'October' => 'Октября', 'Oct' => 'Окт', 'November' => 'Ноября', 'Nov' => 'Ноя', 'December' => 'Декабря', 'Dec' => 'Дек', 'st' => 'ое', 'nd' => 'ое', 'rd' => 'е', 'th' => 'ое');
24:
25: 26: 27: 28:
29:
30: const MINUTE = 60;
31:
32: 33: 34: 35:
36: const HOUR = 3600;
37:
38: 39: 40: 41:
42: const DAY = 86400;
43:
44: 45: 46: 47:
48: const WEEK = 604800;
49:
50: 51: 52: 53:
54: const MONTH = 2629800;
55:
56: 57: 58: 59:
60: const YEAR = 31557600;
61:
62: 63: 64: 65: 66: 67: 68: 69: 70: 71:
72: public static function russian_date()
73: {
74: if (func_num_args() > 1) {
75: $timestamp = func_get_arg(1);
76:
77: return strtr(date(func_get_arg(0), $timestamp), self::$date_translation);
78: } else {
79: return strtr(date(func_get_arg(0)), self::$date_translation);
80: }
81: }
82:
83: 84: 85: 86: 87: 88: 89: 90: 91: 92:
93: public static function month_name_from_index($month)
94: {
95: $month = (int) $month;
96:
97: $all_month = array(
98: 1 => 'Январь',
99: 2 => 'Февраль',
100: 3 => 'Март',
101: 4 => 'Апрель',
102: 5 => 'Май',
103: 6 => 'Июнь',
104: 7 => 'Июль',
105: 8 => 'Август',
106: 9 => 'Сентябрь',
107: 10 => 'Октябрь',
108: 11 => 'Ноябрь',
109: 12 => 'Декабрь'
110: );
111:
112: return $all_month[$month];
113: }
114:
115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125:
126: public static function day_name_from_index($day_index)
127: {
128: $day_index = (int) $day_index;
129:
130: $day_name = array(
131: 1 => 'Понедельник',
132: 2 => 'Вторник',
133: 3 => 'Среда',
134: 4 => 'Четверг',
135: 5 => 'Пятница',
136: 6 => 'Суббота',
137: 7 => 'Воскресение'
138: );
139:
140: return $day_name[$day_index];
141: }
142:
143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154:
155: public static function format($date_time, $format = false)
156: {
157: $format = $format ? $format : JDATE_FORMAT;
158: $datetime = strftime($format, strtotime($date_time));
159:
160: return strtr($datetime, self::$date_translation);
161: }
162:
163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175:
176: public static function current($format = false)
177: {
178: $format = $format ? $format : JDATE_FORMAT;
179: $datetime = strftime($format, time());
180:
181: return strtr($datetime, self::$date_translation);
182: }
183:
184: 185: 186: 187: 188: 189: 190: 191:
192: public static function time_ago($timestamp, $current_time = false)
193: {
194: $timestamp = (int) $timestamp;
195: $current_time = $current_time ? $current_time : time();
196:
197:
198: $seconds_between = $current_time - $timestamp;
199:
200:
201: $years_ago = floor($seconds_between / (365.242199 * 24 * 60 * 60));
202: if ($years_ago > 1) {
203:
204: return sprintf('%s %s назад', $years_ago, joosText::declension($years_ago, array('год', 'года', 'лет')));
205: }
206: if ($years_ago == 1) {
207:
208: return 'год назад';
209: }
210:
211:
212: $months_ago = floor($seconds_between / ((365.242199 / 12) * 24 * 60 * 60));
213: if ($months_ago > 1) {
214:
215: return sprintf('%s %s назад', $months_ago, joosText::declension($months_ago, array('месяц', 'месяца', 'месяцев')));
216: }
217: if ($months_ago == 1) {
218:
219: return 'месяц назад';
220: }
221:
222:
223: $weeks_ago = floor($seconds_between / (7 * 24 * 60 * 60));
224: if ($weeks_ago > 1) {
225:
226: return sprintf('%s %s назад', $weeks_ago, joosText::declension($weeks_ago, array('неделя', 'недели', 'недель')));
227: }
228: if ($weeks_ago == 1) {
229:
230: return 'неделю назад';
231: }
232:
233:
234: $days_ago = floor($seconds_between / (24 * 60 * 60));
235: if ($days_ago > 1) {
236:
237: return sprintf('%s %s назад', $days_ago, joosText::declension($days_ago, array('день', 'дня', 'дней')));
238: }
239: if ($days_ago == 1) {
240:
241: return 'день назад';
242: }
243:
244:
245: $hours_ago = floor($seconds_between / (60 * 60));
246: if ($hours_ago > 1) {
247:
248: return sprintf('%s %s назад', $hours_ago, joosText::declension($hours_ago, array('час', 'часа', 'часов')));
249: }
250: if ($hours_ago == 1) {
251:
252: return 'час назад';
253: }
254:
255:
256: $minutes_ago = floor($seconds_between / 60);
257: if ($minutes_ago > 1) {
258:
259: return sprintf('%s %s назад', $minutes_ago, joosText::declension($minutes_ago, array('минуту', 'минуты', 'минут')));
260: }
261: if ($minutes_ago == 1) {
262:
263: return 'минуту назад';
264: }
265:
266:
267: $seconds_ago = floor($seconds_between);
268: if ($seconds_ago > 1) {
269:
270: return sprintf('%s %s назад', $seconds_ago, joosText::declension($seconds_ago, array('секунда', 'секунд', 'секунды')));
271: }
272: if ($seconds_ago <= 1) {
273:
274: return 'секунда';
275: }
276:
277: return 'Очень давно';
278: }
279:
280: 281: 282: 283: 284: 285:
286: public static function time_string($time)
287: {
288: $time = (int) $time;
289:
290:
291: $years = floor($time / (365.242199 * 24 * 60 * 60));
292: if ($years > 1) {
293:
294: return sprintf('%s %s', $years, joosText::declension($years, array('год', 'года', 'лет')));
295: }
296: if ($years == 1) {
297:
298: return 'год';
299: }
300:
301:
302: $months = floor($time / ((365.242199 / 12) * 24 * 60 * 60));
303: if ($months > 1) {
304:
305: return sprintf('%s %s', $months, joosText::declension($months, array('месяц', 'месяца', 'месяцев')));
306: }
307: if ($months == 1) {
308:
309: return 'месяц';
310: }
311:
312:
313: $weeks = floor($time / (7 * 24 * 60 * 60));
314: if ($weeks > 1) {
315:
316: return sprintf('%s %s', $weeks, joosText::declension($weeks, array('неделя', 'недели', 'недель')));
317: }
318: if ($weeks == 1) {
319:
320: return 'неделю';
321: }
322:
323:
324: $days = floor($time / (24 * 60 * 60));
325: if ($days > 1) {
326:
327: return sprintf('%s %s', $days, joosText::declension($days, array('день', 'дня', 'дней')));
328: }
329: if ($days == 1) {
330:
331: return 'день';
332: }
333:
334:
335: $hours = floor($time / (60 * 60));
336: if ($hours > 1) {
337:
338: return sprintf('%s %s', $hours, joosText::declension($hours, array('час', 'часа', 'часов')));
339: }
340: if ($hours == 1) {
341:
342: return 'час';
343: }
344:
345:
346: $minutes = floor($time / 60);
347: if ($minutes > 1) {
348:
349: return sprintf('%s %s', $minutes, joosText::declension($minutes, array('минуту', 'минуты', 'минут')));
350: }
351: if ($minutes == 1) {
352:
353: return 'минута';
354: }
355: }
356:
357: public static function standard_date($fmt = 'DATE_RFC822', $time = '')
358: {
359: $formats = array(
360: 'DATE_ATOM' => '%Y-%m-%dT%H:%i:%s%Q',
361: 'DATE_COOKIE' => '%l, %d-%M-%y %H:%i:%s UTC',
362: 'DATE_ISO8601' => '%Y-%m-%dT%H:%i:%s%Q',
363: 'DATE_RFC822' => '%D, %d %M %y %H:%i:%s %O',
364: 'DATE_RFC850' => '%l, %d-%M-%y %H:%i:%s UTC',
365: 'DATE_RFC1036' => '%D, %d %M %y %H:%i:%s %O',
366: 'DATE_RFC1123' => '%D, %d %M %Y %H:%i:%s %O',
367: 'DATE_RSS' => '%D, %d %M %Y %H:%i:%s %O',
368: 'DATE_W3C' => '%Y-%m-%dT%H:%i:%s%Q'
369: );
370:
371: if (!isset($formats[$fmt])) {
372:
373: return FALSE;
374: }
375:
376: return self::russian_date($formats[$fmt], $time);
377: }
378:
379: }
380: