[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/HTML/Helpers/ -> Date.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2011 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\HTML\Helpers;
  11  
  12  use Joomla\CMS\Date\Date as DateHelper;
  13  use Joomla\CMS\HTML\HTMLHelper;
  14  use Joomla\CMS\Language\Text;
  15  
  16  // phpcs:disable PSR1.Files.SideEffects
  17  \defined('JPATH_PLATFORM') or die;
  18  // phpcs:enable PSR1.Files.SideEffects
  19  
  20  /**
  21   * Extended Utility class for handling date display.
  22   *
  23   * @since  2.5
  24   */
  25  abstract class Date
  26  {
  27      /**
  28       * Function to convert a static time into a relative measurement
  29       *
  30       * @param   string  $date    The date to convert
  31       * @param   string  $unit    The optional unit of measurement to return
  32       *                           if the value of the diff is greater than one
  33       * @param   string  $time    An optional time to compare to, defaults to now
  34       * @param   string  $format  An optional format for the HTMLHelper::date output
  35       *
  36       * @return  string  The converted time string
  37       *
  38       * @since   2.5
  39       */
  40      public static function relative($date, $unit = null, $time = null, $format = null)
  41      {
  42          if ($time === null) {
  43              // Get now
  44              $time = new DateHelper('now');
  45          }
  46  
  47          // Get the difference in seconds between now and the time
  48          $diff = strtotime($time) - strtotime($date);
  49  
  50          // Less than a minute
  51          if ($diff < 60) {
  52              return Text::_('JLIB_HTML_DATE_RELATIVE_LESSTHANAMINUTE');
  53          }
  54  
  55          // Round to minutes
  56          $diff = round($diff / 60);
  57  
  58          // 1 to 59 minutes
  59          if ($diff < 60 || $unit === 'minute') {
  60              return Text::plural('JLIB_HTML_DATE_RELATIVE_MINUTES', $diff);
  61          }
  62  
  63          // Round to hours
  64          $diff = round($diff / 60);
  65  
  66          // 1 to 23 hours
  67          if ($diff < 24 || $unit === 'hour') {
  68              return Text::plural('JLIB_HTML_DATE_RELATIVE_HOURS', $diff);
  69          }
  70  
  71          // Round to days
  72          $diff = round($diff / 24);
  73  
  74          // 1 to 6 days
  75          if ($diff < 7 || $unit === 'day') {
  76              return Text::plural('JLIB_HTML_DATE_RELATIVE_DAYS', $diff);
  77          }
  78  
  79          // Round to weeks
  80          $diff = round($diff / 7);
  81  
  82          // 1 to 4 weeks
  83          if ($diff <= 4 || $unit === 'week') {
  84              return Text::plural('JLIB_HTML_DATE_RELATIVE_WEEKS', $diff);
  85          }
  86  
  87          // Over a month, return the absolute time
  88          return HTMLHelper::_('date', $date, $format);
  89      }
  90  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer