Answer the question
In order to leave comments, you need to log in
How to get publication date in format 01:17 minutes ago?
Hello everyone !
I decided to display the publication date in the format (as an example) 01:17 minutes ago. For this, in the overridden file
templates/my_template/html/layouts/joomla/content/info_block/publish_date.php
<span uk-icon="icon: calendar"></span>
<time datetime="<?php echo JHtml::_('date', $displayData['item']->publish_up, 'c'); ?>" itemprop="datePublished">
<?php
$time_ago = strtotime($displayData['item']->publish_up);
$current_time = time();
$time_difference = $current_time - $time_ago;
$seconds = $time_difference;
$minutes = round($seconds / 60); // 60 secund
$hours = round($seconds / 3600); //3600 secund w 60 minutah * 60 secund
if ($seconds <= 60){
echo JText::_('COM_CCK_JUST_NOW');
} else if ($minutes <= 60){
if ($minutes == 1){
echo JText::_('COM_CCK_JUST_NOW');
} else if ($minutes <= 4 || $minutes >= 22 && $minutes <= 24 || $minutes >= 32 && $minutes <= 34 || $minutes >= 42 && $minutes <= 44 || $minutes >= 52 && $minutes <= 54){
echo $minutes . " " . JText::_('COM_CCK_MINUTES') . " " . JText::_('COM_CCK_AGO');
} else if ($minutes >= 5 && $minutes <= 20 || $minutes >= 25 && $minutes <= 30 || $minutes >= 35 && $minutes <= 40 || $minutes >= 45 && $minutes <= 50 || $minutes >= 55 && $minutes <= 60){
echo $minutes . " " . JText::_('COM_CCK_MINUT') . " " . JText::_('COM_CCK_AGO');
} else if ($minutes == 21 || $minutes == 31 || $minutes == 41 || $minutes == 51){
echo $minutes . " " . JText::_('COM_CCK_MINUTE') . " " . JText::_('COM_CCK_AGO');
}
} else if ($hours <= 24){
if ($hours <= 9) {
echo "0" . $hours . ":" . $minutes . " " . JText::_('COM_CCK_AGO');
}
else {
echo $hours . ":" . $minutes . " " . JText::_('COM_CCK_AGO');
}
} else {
echo "<span>" . JHtml::_('date', $displayData['item']->publish_up, JText::_('DATE_FORMAT_LC2')) . "</span>";
}
?>
</time>
} else if ($hours <= 24){
if ($hours <= 9) {
echo "0" . $hours . ":" . $minutes . " " . JText::_('COM_CCK_AGO');
}
else {
echo $hours . ":" . $minutes . " " . JText::_('COM_CCK_AGO');
}
Answer the question
In order to leave comments, you need to log in
The solution to the problem is:
In the file
I wrote the following function call code:
require_once JPATH_THEMES . '/my_teplate/helper.php'; // подключаю файл с функцией
<span uk-icon="icon: calendar"></span>
<time datetime="<?php echo JHtml::_('date', $art_publish_date, 'c'); ?>" itemprop="datePublished">
<?php echo publish_date ($art_publish_date); ?>
</time>
function publish_date($timestamp) {
$current_time = time();
$publish_time = strtotime($timestamp);
$time_diff = $current_time - $publish_time;
$seconds = $time_diff;
$minutes = round($seconds / 60); // 60 секунд. Минуты в UNIX формате даты
$days = JHtml::_('date', $current_time, 'd') - JHtml::_('date', $publish_time, 'd');
$minutes_interval = $minutes >= 2 && $minutes <= 4 || $minutes >= 22 && $minutes <= 24 || $minutes >= 32 && $minutes <= 34 || $minutes >= 42 && $minutes <= 44 || $minutes >= 52 && $minutes <= 54;
$minut_interval = $minutes == 0 || $minutes >= 5 && $minutes <= 20 || $minutes >= 25 && $minutes <= 30 || $minutes >= 35 && $minutes <= 40 || $minutes >= 45 && $minutes <= 50 || $minutes >= 55 && $minutes <= 60;
$minuta_interval = $minutes == 1 | $minutes == 21 || $minutes == 31 || $minutes == 41 || $minutes == 51;
if ($seconds <= 60) {
return "<span>" . JText::_('COM_CCK_JUST_NOW') . "</span>";
} else if ($minutes <= 59) {
if ($minutes == 1) {
return "<span>" . JText::_('COM_CCK_JUST_NOW') . "</span>";
} else if ($minutes_interval) {
return "<span>" . $minutes . " " . JText::_('COM_CCK_MINUTES') . " " . JText::_('COM_CCK_AGO') . "</span>";
} else if ($minut_interval) {
return "<span>" . $minutes . " " . JText::_('COM_CCK_MINUT') . " " . JText::_('COM_CCK_AGO') . "</span>";
} else if ($minuta_interval) {
return "<span>" . $minutes . " " . JText::_('COM_CCK_MINUTE') . " " . JText::_('COM_CCK_AGO') . "</span>";
}
} else if ($days == 0) {
return "<span>" . JText::_('COM_CCK_TODAY_AT') . " " . JHtml::_('date', $timestamp, JText::_('DATE_FORMAT_LC12')) . "</span>";
}else if ($days == 1) {
return "<span>" . JText::_('COM_CCK_YESTERDAY_IN') . " " . JHtml::_('date', $timestamp, JText::_('DATE_FORMAT_LC12')) . "</span>";
} else {
return "<span>" . JHtml::_('date', $timestamp, JText::_('DATE_FORMAT_LC2')) . "</span>";
}
}
DATE_FORMAT_LC12="H:i"
COM_CCK_JUST_NOW="опубликовано только что"
COM_CCK_MINUT="минут"
COM_CCK_MINUTE="минута"
COM_CCK_MINUTES="минуты"
COM_CCK_AGO="назад"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question