| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikWP - Libraries |
| 4 |
* @subpackage adapter.html |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved. |
| 7 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
/** |
| 15 |
* Extended Utility class for handling date display. |
| 16 |
* |
| 17 |
* @since 10.1.19 |
| 18 |
*/ |
| 19 |
abstract class JHtmlDate |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Function to convert a static time into a relative measurement. |
| 23 |
* |
| 24 |
* @param mixed $date The date to convert. |
| 25 |
* @param string $unit The optional unit of measurement to return |
| 26 |
* if the value of the diff is greater than one. |
| 27 |
* @param string $time An optional time to compare to, defaults to now. |
| 28 |
* @param string $format An optional format for the JHtml::date output. |
| 29 |
* |
| 30 |
* @return string The converted time string. |
| 31 |
*/ |
| 32 |
public static function relative($date, $unit = null, $time = null, $format = null) |
| 33 |
{ |
| 34 |
if ($time === null) |
| 35 |
{ |
| 36 |
// get now |
| 37 |
$time = new JDate('now'); |
| 38 |
} |
| 39 |
|
| 40 |
// get the difference in seconds between now and the time |
| 41 |
$diff = strtotime($time) - strtotime($date); |
| 42 |
|
| 43 |
// less than a minute |
| 44 |
if ($diff < 60) |
| 45 |
{ |
| 46 |
return JText::translate('JLIB_HTML_DATE_RELATIVE_LESSTHANAMINUTE'); |
| 47 |
} |
| 48 |
|
| 49 |
// round to minutes |
| 50 |
$diff = round($diff / 60); |
| 51 |
|
| 52 |
// 1 to 59 minutes |
| 53 |
if ($diff < 60 || $unit === 'minute') |
| 54 |
{ |
| 55 |
return JText::plural('JLIB_HTML_DATE_RELATIVE_MINUTES', $diff); |
| 56 |
} |
| 57 |
|
| 58 |
// round to hours |
| 59 |
$diff = round($diff / 60); |
| 60 |
|
| 61 |
// 1 to 23 hours |
| 62 |
if ($diff < 24 || $unit === 'hour') |
| 63 |
{ |
| 64 |
return JText::plural('JLIB_HTML_DATE_RELATIVE_HOURS', $diff); |
| 65 |
} |
| 66 |
|
| 67 |
// round to days |
| 68 |
$diff = round($diff / 24); |
| 69 |
|
| 70 |
// 1 to 6 days |
| 71 |
if ($diff < 7 || $unit === 'day') |
| 72 |
{ |
| 73 |
return JText::plural('JLIB_HTML_DATE_RELATIVE_DAYS', $diff); |
| 74 |
} |
| 75 |
|
| 76 |
// round to weeks |
| 77 |
$diff = round($diff / 7); |
| 78 |
|
| 79 |
// 1 to 4 weeks |
| 80 |
if ($diff <= 4 || $unit === 'week') |
| 81 |
{ |
| 82 |
return JText::plural('JLIB_HTML_DATE_RELATIVE_WEEKS', $diff); |
| 83 |
} |
| 84 |
|
| 85 |
// over a month, return the absolute time |
| 86 |
return JHtml::fetch('date', $date, $format); |
| 87 |
} |
| 88 |
} |
| 89 |
|