Created
July 14, 2016 10:44
-
-
Save oxyberg/dbf4f461ad486fa06c50854a270dd671 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function relative($time) { | |
$time = [ | |
'year' => 2015, | |
'month' => 06, | |
'day' => 07, | |
'hours' => 10, | |
'minutes' => 12, | |
'seconds' => 46 | |
]; | |
$seconds = $time['hours'] * 60 * 60 + $time['minutes'] * 60 + $time['seconds']; | |
$current = date('H') * 60 * 60 + date('i') * 60 + date('s'); | |
$months = [ | |
01 => 'января', | |
02 => 'февраля', | |
03 => 'марта', | |
04 => 'апреля', | |
05 => 'мая', | |
06 => 'июня', | |
07 => 'июля', | |
08 => 'августа', | |
09 => 'сентября', | |
10 => 'октября', | |
11 => 'ноября', | |
12 => 'декабря' | |
]; | |
// если не в этом году | |
if ($time['year'] != date('Y')) { | |
return $time['day'] . ' ' . $months[$time['month']] . ' ' . $time['year'] . ', в ' . $time['hours'] . ':' . $time['minutes']; | |
} | |
// если не в этом месяце | |
if ($time['month'] != date('m')) { | |
return $time['day'] . ' ' . $months[$time['month']] . ' в ' . $time['hours'] . ':' . $time['minutes']; | |
} | |
// если вчера | |
if (date('d') == $time['day']+1) { | |
return 'вчера в ' . $time['hours'] . ':' . $time['minutes']; | |
} | |
// меньше минуты назад | |
if (($current - $seconds) < 60) { | |
return 'только что'; | |
} | |
// меньше часа назад | |
if (($current - $seconds) < 60 * 60) { | |
$ending = substr((string)round(($current - $seconds) / 60), 1); | |
if ($ending == 1) $end = 'а'; // 1 | |
else if (in_array($ending, [11, 12, 13, 14, 15, 16, 17, 18, 19])) $end = ''; // 11, 12, 13, 14, 15, 16, 17, 18, 19 | |
else if (in_array($ending, [2, 3, 4])) $end = 'ы'; // 2, 3, 4 | |
else $end = ''; // 5, 6, 7, 8, 9, 0 | |
return round(($current - $seconds) / 60) . ' минут' . $end . ' назад'; | |
} | |
// 1 час — 1 час 30 минут | |
if ((($current - $seconds) > 60 * 60) && (($current - $seconds) < 60 * 60 + 60 * 30)) { | |
return 'час назад'; | |
} | |
// 1 час 30 минут — 2 часа 30 минут | |
if ((($current - $seconds) > 60 * 60 + 60 * 30) && (($current - $seconds) < 60 * 60 * 2 + 60 * 30)) { | |
return 'два часа назад'; | |
} | |
if (($current - $seconds) > 60 * 60 * 2 + 60 * 30) { | |
return 'сегодня в ' . $time['hours'] . ':' . $time['minutes']; | |
} | |
return $time['day'] . ' ' . $months[$time['month']] . ' в ' . $time['hours'] . ':' . $time['minutes']; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment