Created
October 31, 2017 13:32
-
-
Save PandCar/b5504cf22d73d8af39b82ec1c00b3f10 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 | |
/** | |
* Автор Олег Исаев | |
* ВКонтакте: vk.com/id50416641 | |
* Skype: pandcar97 | |
*/ | |
define('TIME', time()); | |
echo '<script src="RelativeTime.js"></script>'; | |
echo data_form($timestamp); | |
// Вывод относительного времени | |
function data_form($timestamp) | |
{ | |
$str = ''; | |
$month_list = ['января','февраля','марта','апреля','мая','июня','июля','августа','сентября','октября','ноября','декабря']; | |
list($day, $month, $year, $hour, $minute) = explode('-', date('j-n-Y-G-i', $timestamp)); | |
if ($day.$month.$year == date('jnY', TIME)) | |
{ | |
$sec_passed = TIME - $timestamp; | |
if (3600 > $sec_passed) | |
{ | |
if (60 > $sec_passed) | |
{ | |
$str = 'Только что'; | |
} | |
elseif (120 > $sec_passed) | |
{ | |
$str = 'Минуту назад'; | |
} | |
elseif (180 > $sec_passed) | |
{ | |
$str = 'Две минуты назад'; | |
} | |
else | |
{ | |
$str = decline(intval($sec_passed / 60), ['минуту','минуты','минут']).' назад'; | |
} | |
} | |
else | |
{ | |
$str = 'Сегодня в '.$hour.':'.$minute; | |
} | |
} | |
elseif ($day.$month.$year == date('jnY', TIME - (3600 * 24))) | |
{ | |
$str = 'Вчера в '.$hour.':'.$minute; | |
} | |
elseif ($year == date('Y', TIME)) | |
{ | |
$str = $day.' '.$month_list[ $month - 1 ].' в '.$hour.':'.$minute; | |
} | |
else | |
{ | |
$str = $day.' '.$month_list[ $month - 1 ].' '.$year.' в '.$hour.':'.$minute; | |
} | |
return '<span class="relative-time" data-time="'.$timestamp.'">'.$str.'</span>'; | |
} |
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
/** | |
* Автор Олег Исаев | |
* ВКонтакте: vk.com/id50416641 | |
* Skype: pandcar97 | |
*/ | |
(function() { | |
var month_list = ['января','февраля','марта','апреля','мая','июня','июля','августа','сентября','октября','ноября','декабря']; | |
var cases = [2, 0, 1, 1, 1, 2]; | |
setInterval( | |
function(){ | |
var els = document.getElementsByClassName("relative-time"); | |
for (var key = 0; key < els.length; key++) { | |
var ts = els[key].getAttribute("data-time"); | |
els[key].innerHTML = RelativeTime(ts); | |
} | |
}, | |
3000 | |
); | |
var RelativeTime = function(timestamp) { | |
timestamp = parseInt(timestamp); | |
var inputTS = new Date(timestamp * 1000); | |
var nowTS = new Date(); | |
var dayAgoTS = new Date(); | |
dayAgoTS.setDate(dayAgoTS.getDate()-1); | |
var day = inputTS.getDate(); | |
var month = inputTS.getMonth(); | |
var year = inputTS.getFullYear(); | |
var hour = inputTS.getHours(); | |
var minute = inputTS.getMinutes(); | |
if (hour < 10) hour = '0' + hour; | |
if (minute < 10) minute = '0' + minute; | |
if (day + "-" + month + "-" + year == nowTS.getDate() + "-" + nowTS.getMonth() + "-" + nowTS.getFullYear()) { | |
var sec_passed = parseInt(nowTS.getTime() / 1000) - timestamp; | |
if (3600 > sec_passed) { | |
if (60 > sec_passed) { | |
return "Только что"; | |
} | |
else if (120 > sec_passed) { | |
return "Минуту назад"; | |
} | |
else if (180 > sec_passed) { | |
return "Две минуты назад"; | |
} | |
else { | |
return decline(parseInt(sec_passed / 60), ["минуту","минуты","минут"]) + " назад"; | |
} | |
} | |
else { | |
return "Сегодня в " + hour + ':' + minute; | |
} | |
} | |
else if (day + "-" + month + "-" + year == dayAgoTS.getDate() + "-" + dayAgoTS.getMonth() + "-" + dayAgoTS.getFullYear()) { | |
return "Вчера в " + hour + ":" + minute; | |
} | |
else if (year == nowTS.getFullYear()) { | |
//if (day < 10) day = '0' + day; | |
return day + " " + month_list[ month ] + " в " + hour + ":" + minute; | |
} | |
else { | |
//if (day < 10) day = '0' + day; | |
return day + " " + month_list[ month ] + " " + year + " в " + hour + ":" + minute; | |
} | |
} | |
var decline = function(number, after) { | |
var tmpn = number % 10; | |
var result = number + " " + after[ ((number % 100 > 4 && number % 100 < 20) ? 2 : cases[ (tmpn < 5 ? tmpn : 5) ]) ]; | |
return result; | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment