1 <?php
2 /**
3 * @package Joomla.Libraries
4 * @subpackage HTML
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE.txt
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 /**
13 * Extended Utility class for handling date display.
14 *
15 * @since 2.5
16 */
17 abstract class JHtmlDate
18 {
19 /**
20 * Function to convert a static time into a relative measurement
21 *
22 * @param string $date The date to convert
23 * @param string $unit The optional unit of measurement to return
24 * if the value of the diff is greater than one
25 * @param string $time An optional time to compare to, defaults to now
26 * @param string $format An optional format for the JHtml::date output
27 *
28 * @return string The converted time string
29 *
30 * @since 2.5
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::_('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::_('date', $date, $format);
87 }
88 }
89