1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) Ouzo contributors, http://ouzoframework.org |
4
|
|
|
* This file is made available under the MIT License (view the LICENSE file for more information). |
5
|
|
|
*/ |
6
|
|
|
namespace Ouzo\Utilities; |
7
|
|
|
|
8
|
|
|
use DateTime; |
9
|
|
|
|
10
|
|
|
class TimeAgo |
11
|
|
|
{ |
12
|
|
|
private $_date; |
13
|
|
|
|
14
|
|
|
private $key; |
15
|
|
|
private $params = array(); |
16
|
|
|
|
17
|
|
|
public function __construct($date) |
18
|
|
|
{ |
19
|
|
|
$this->_date = $date; |
20
|
|
|
$this->prepare(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
private function prepare() |
24
|
|
|
{ |
25
|
|
|
$date = new DateTime($this->_date); |
26
|
|
|
if ($this->_showJustNow()) { |
27
|
|
|
$this->key = 'timeAgo.justNow'; |
28
|
|
|
return; |
29
|
|
|
} |
30
|
|
|
if ($minutesAgo = $this->_showMinutesAgo()) { |
31
|
|
|
$this->key = 'timeAgo.minAgo'; |
32
|
|
|
$this->params = array('label' => $minutesAgo); |
33
|
|
|
return; |
34
|
|
|
} |
35
|
|
|
if ($this->_showTodayAt()) { |
36
|
|
|
$this->key = 'timeAgo.todayAt'; |
37
|
|
|
$this->params = array('label' => $date->format('H:i')); |
38
|
|
|
return; |
39
|
|
|
} |
40
|
|
|
if ($this->_showYesterdayAt()) { |
41
|
|
|
$this->key = 'timeAgo.yesterdayAt'; |
42
|
|
|
$this->params = array('label' => $date->format('H:i')); |
43
|
|
|
return; |
44
|
|
|
} |
45
|
|
|
if ($this->_showThisYear()) { |
46
|
|
|
$this->key = 'timeAgo.thisYear'; |
47
|
|
|
$this->params = array('day' => $date->format('j'), 'month' => 'timeAgo.month.' . $date->format('n')); |
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
$this->key = $date->format('Y-m-d'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
private function _showJustNow() |
54
|
|
|
{ |
55
|
|
|
return $this->_getDateDiff() <= 60; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private function _showMinutesAgo() |
59
|
|
|
{ |
60
|
|
|
$difference = $this->_getDateDiff(); |
61
|
|
|
return ($difference > 60 && $difference < 3600) ? floor($difference / 60) : null; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function _showTodayAt() |
65
|
|
|
{ |
66
|
|
|
$difference = $this->_getDateDiff(); |
67
|
|
|
return $this->_isSameDay() && $difference >= 3600 && $difference < 86400; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function _getDateDiff() |
71
|
|
|
{ |
72
|
|
|
return $this->_nowAsTimestamp() - $this->_dateAsTimestamp(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function _nowAsTimestamp() |
76
|
|
|
{ |
77
|
|
|
return Clock::now()->getTimestamp(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function _dateAsTimestamp() |
81
|
|
|
{ |
82
|
|
|
return strtotime($this->_date); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function _isSameDay() |
86
|
|
|
{ |
87
|
|
|
$now = $this->_nowAsTimestamp(); |
88
|
|
|
$date = $this->_dateAsTimestamp(); |
89
|
|
|
return date('Y-m-d', $now) == date('Y-m-d', $date); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function _showYesterdayAt() |
93
|
|
|
{ |
94
|
|
|
$now = $this->_nowAsTimestamp(); |
95
|
|
|
$date = $this->_dateAsTimestamp(); |
96
|
|
|
if (date('Y-m', $now) == date('Y-m', $date)) { |
97
|
|
|
return date('d', $now) - date('d', $date) == 1; |
98
|
|
|
} |
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private function _showThisYear() |
103
|
|
|
{ |
104
|
|
|
$now = $this->_nowAsTimestamp(); |
105
|
|
|
$date = $this->_dateAsTimestamp(); |
106
|
|
|
return date('Y', $now) == date('Y', $date); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public static function create($date) |
110
|
|
|
{ |
111
|
|
|
return new self($date); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function getKey() |
115
|
|
|
{ |
116
|
|
|
return $this->key; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function getParams() |
120
|
|
|
{ |
121
|
|
|
return $this->params; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|