A
A
Alexon Classic2019-01-16 04:38:59
Joomla
Alexon Classic, 2019-01-16 04:38:59

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

I have written this code:
<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>

as a result, if the article was published less than 60 minutes, then everything is displayed perfectly! For example: 31 minutes ago or 50 minutes ago...
Now I want to make it so that if the article was published more than an hour ago, then the time is displayed in this format: ##:## minutes ago (i.e. 01:17 minutes ago )... But something doesn't work for me with this code:
} else if ($hours <= 24){
      if ($hours <= 9) {
        echo "0" . $hours . ":" . $minutes . " " . JText::_('COM_CCK_AGO');
      }
      else {
        echo $hours . ":" . $minutes . " " . JText::_('COM_CCK_AGO');
      }

i.e. displays the clock - everything is OK! But it's a problem with minutes... I understand that I have minutes in UNIX format and I need to convert them somehow into a human form... But I don't understand how to do it...
Help please!
Thanks in advance for your replies !

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexon Classic, 2019-01-21
@AlexonClassic

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>

At the root of your template. I created a file:
In which I registered this function:
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>";
    }
  }

Added several language variables to
DATE_FORMAT_LC12="H:i"
COM_CCK_JUST_NOW="опубликовано только что"
COM_CCK_MINUT="минут"
COM_CCK_MINUTE="минута"
COM_CCK_MINUTES="минуты"
COM_CCK_AGO="назад"

Eventually:
  • if the article was published less than 59 minutes, then it is displayed (for example): 31 minutes A ago or 50 minutes ago...
  • if the article has been published for more than 60 minutes, then it is displayed (for example): today. at 01:25...
  • if the article was published more than a day ago, then it is displayed: yesterday, at 15:27...

Y
Yakov Vylegzhanin, 2019-01-16
@vylegzhanin

Why fence the bike? There is moment.js for this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question