|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OpenCafe\Tools; |
|
4
|
|
|
|
|
5
|
|
|
use OpenCafe\Tools\Lang; |
|
6
|
|
|
use OpenCafe\Datium; |
|
7
|
|
|
use stdClass; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Calculate Time ago with current date difference |
|
11
|
|
|
* |
|
12
|
|
|
* @package OpenCafe\Datium |
|
13
|
|
|
* @since Jun 17, 2016 |
|
14
|
|
|
*/ |
|
15
|
|
|
class TimeAgo |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
private $language; |
|
19
|
|
|
|
|
20
|
|
|
private $output; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* TimeAgo Class constructure |
|
24
|
|
|
* |
|
25
|
|
|
* @param DateTime $time_difference The time to calculate with now |
|
26
|
|
|
* @param string $lang Language |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct($time_difference, $lang ) |
|
29
|
|
|
{ |
|
30
|
|
|
|
|
31
|
|
|
$this->language = $lang; |
|
32
|
|
|
|
|
33
|
|
|
$now = Datium::now()->object(); |
|
34
|
|
|
|
|
35
|
|
|
$this->output = Datium::diff( $now, $time_difference ); |
|
36
|
|
|
|
|
37
|
|
|
$this->year = $this->output->year; |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
$this->month = $this->output->month; |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
$this->day = $this->output->day; |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
$this->hour = $this->output->hour; |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
$this->minute = $this->output->minute; |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
$this->second = $this->output->second; |
|
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Return difference period as an object |
|
53
|
|
|
* |
|
54
|
|
|
* @return object |
|
55
|
|
|
*/ |
|
56
|
|
|
public function all() |
|
57
|
|
|
{ |
|
58
|
|
|
|
|
59
|
|
|
return $result->output; |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Read ago config file |
|
65
|
|
|
* |
|
66
|
|
|
* @param integer $date date index in ago config file |
|
|
|
|
|
|
67
|
|
|
* @param string $type duration type on ago config file |
|
68
|
|
|
*/ |
|
69
|
|
|
public function read( $value, $type ) |
|
70
|
|
|
{ |
|
71
|
|
|
|
|
72
|
|
|
$config = include 'config/ago.php'; |
|
73
|
|
|
|
|
74
|
|
|
if( array_key_exists( $value, $config[ $type ] ) ) { |
|
75
|
|
|
|
|
76
|
|
|
return Lang::get( $this->language, $config[ $type ][ $value ] ); |
|
77
|
|
|
|
|
78
|
|
|
} else { |
|
79
|
|
|
|
|
80
|
|
|
return $value . " " . Lang::get( $this->language, $config[ $type ][ '*' ] ); |
|
81
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Show priority of duration |
|
88
|
|
|
* |
|
89
|
|
|
* @param integer $date date index in ago config file |
|
90
|
|
|
* @param string $type duration type on ago config file |
|
91
|
|
|
*/ |
|
92
|
|
|
public function priority( $date, $type ) |
|
93
|
|
|
{ |
|
94
|
|
|
|
|
95
|
|
|
if( $date != 0 ) { |
|
96
|
|
|
|
|
97
|
|
|
return $this->read( $date, $type ); |
|
98
|
|
|
|
|
99
|
|
|
} else { |
|
100
|
|
|
|
|
101
|
|
|
return false; |
|
102
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Return fainal TimeAgo result |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
|
|
public function get() |
|
113
|
|
|
{ |
|
114
|
|
|
|
|
115
|
|
|
$duration = [ |
|
116
|
|
|
'year' => $this->year, |
|
117
|
|
|
'month' => $this->month, |
|
118
|
|
|
'day' => $this->day, |
|
119
|
|
|
'hour' => $this->hour, |
|
120
|
|
|
'minute' => $this->minute, |
|
121
|
|
|
'second' => $this->second |
|
122
|
|
|
]; |
|
123
|
|
|
|
|
124
|
|
|
foreach( $duration as $index => $value ) { |
|
125
|
|
|
|
|
126
|
|
|
if( $this->priority( $value, $index ) != false ) { |
|
127
|
|
|
|
|
128
|
|
|
return $this->priority( $value, $index ); |
|
129
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: