This repository has been archived by the owner on May 31, 2020. It is now read-only.
/
RelativeTime.php
executable file
·153 lines (137 loc) · 4.06 KB
/
RelativeTime.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
* RelativeTime.php
*
* @author Michael Pratt <pratt@hablarmierda.net>
* @link http://www.michael-pratt.com/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace RelativeTime;
/**
* The Main Class of the library
*/
class RelativeTime
{
/** @var int Class constant with the current Version of this library */
const VERSION = '1.0';
/** @var array Array With configuration options **/
protected $config = array();
/** @var object instance of \Relativetime\Translation **/
protected $translation;
/**
* Construct
*
* @param array $config Associative array with configuration directives
* @return string
*/
public function __construct(array $config = array())
{
$this->config = array_merge(array(
'language' => '\RelativeTime\Languages\English',
'separator' => ', ',
'suffix' => true,
'truncate' => 0,
), $config);
$this->translation = new \RelativeTime\Translation($this->config);
}
/**
* Converts 2 dates to its relative time.
*
* @param string $fromTime
* @param string $toTime When null is given, uses the current date.
* @return string
*/
public function convert($fromTime, $toTime = null)
{
$interval = $this->getInterval($fromTime, $toTime);
$units = $this->calculateUnits($interval);
return $this->translation->translate($units, $interval->invert);
}
/**
* Tells the time passed between the current date and the given date
*
* @param string $date
* @return string
*/
public function TimeAgo($date)
{
$interval = $this->getInterval(time(), $date);
if ($interval->invert) {
return $this->convert(time(), $date);
}
return $this->translation->translate();
}
/**
* Tells the time until the given date
*
* @param string $date
* @return string
*/
public function TimeLeft($date)
{
$interval = $this->getInterval($date, time());
if ($interval->invert) {
return $this->convert(time(), $date);
}
return $this->translation->translate();
}
/**
* Calculates the interval between the dates and returns
* an array with the valid time.
*
* @param string $fromTime
* @param string $toTime When null is given, uses the current date.
* @return \DateInterval
*/
protected function getInterval($fromTime, $toTime = null)
{
$fromTime = new \DateTime($this->normalizeDate($fromTime));
$toTime = new \DateTime($this->normalizeDate($toTime));
return $fromTime->diff($toTime);
}
/**
* Normalizes a date for the \DateTime class
*
* @param string $date
* @return string
*/
protected function normalizeDate($date)
{
$date = str_replace(array('/', '|'), '-', $date);
if (empty($date)) {
return date('Y-m-d H:i:s');
}else if (ctype_digit($date)) {
return date('Y-m-d H:i:s', $date);
}
return $date;
}
/**
* Given a DateInterval, creates an array with the time
* units and truncates it when necesary.
*
* @param \DateInterval $interval Instance of \DateInterval
* @return array
*/
protected function calculateUnits(\DateInterval $interval)
{
$units = array_filter(array(
'years' => (int) $interval->y,
'months' => (int) $interval->m,
'days' => (int) $interval->d,
'hours' => (int) $interval->h,
'minutes' => (int) $interval->i,
'seconds' => (int) $interval->s,
));
if (empty($units)) {
return array();
}else if ($this->config['truncate'] > 0) {
return array_slice($units, 0, $this->config['truncate']);
}else {
return $units;
}
}
}
?>