/
Time.php
158 lines (142 loc) · 4.26 KB
/
Time.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
154
155
156
157
158
<?php
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @copyright 2010 onwards James McQuillan (http://pdyn.net)
* @author James McQuillan <james@pdyn.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace pdyn\datatype;
class Time extends \DateTime {
const YYYYMMDD = 'Ymd';
const longDate = 'l, F j, Y';
const longDateTime = 'l, F j, Y H:i:s';
const mysql = 'r';
const standards = 'r';
const YYYY = 'Y';
const MM = 'm';
const DD = 'd';
public function __construct($time = 'now', \DateTimeZone $timezone = null) {
if ((string)(int)$time === (string)$time) {
$time = '@'.$time;
}
return parent::__construct($time, $timezone);
}
/**
* Magic method for string usage.
*
* @return string The result of the val() method.
*/
public function __toString() {
return $this->getTimestamp();
}
/**
* Create a time object from a human-readable date string, parseable by strtotime().
*
* @param string $stringdate String date.
* @return \pdyn\datatype\Time Time object.
*/
public static function createFromString($stringdate) {
return new static(strtotime($stringdate));
}
/**
* Get a string showing the difference between the stored time and the current (or specified) time.
*
* This generates strings like "5 minutes ago", "10 hours ago", etc.
*
* @param string $datestyle If the difference is greater than 7 days, it will fall back to a formatted date using this format.
* @param int $now Specify a timestamp to be used as "now".
* @return string The generated relative time string.
*/
public function get_relative_time($datestyle = null, $now = null) {
if ($datestyle === 'long') {
$datestyle = 'l, F j, Y H:i:s';
}
$time = $this->getTimestamp();
if (empty($time)) {
return 'Never';
}
if (empty($now) || !is_int($now)) {
$now = time();
}
$str = [
'second' => '%d second',
'seconds' => '%d seconds',
'minute' => '%d minute',
'minutes' => '%d minutes',
'hour' => '%d hour',
'hours' => '%d hours',
'day' => '%d day',
'days' => '%d days',
'fromnow' => ' from now',
'ago' => ' ago',
];
$str = [
'second' => '%ds',
'seconds' => '%ds',
'minute' => '%dm',
'minutes' => '%dm',
'hour' => '%dh',
'hours' => '%dh',
'day' => '%dd',
'days' => '%dd',
'fromnow' => '',
'ago' => '',
];
$diff = $now - $time;
$diffabs = abs($diff);
$relative = true;
$out = '';
if ($relative === true) {
if ($diffabs == 0) {
$out = 'now';
} elseif ($diffabs < 60) {
// Second accuracy.
$out = ($diffabs == 1) ? sprintf($str['second'], $diffabs) : sprintf($str['seconds'], $diffabs);
} elseif ($diffabs >= 60 && $diffabs < 3600) {
// Minute accuracy.
$mins = (int)round($diffabs / 60);
$out = ($mins === 1) ? sprintf($str['minute'], $mins) : sprintf($str['minutes'], $mins);
} elseif ($diffabs >= 3600 && $diffabs < 86400) {
// Hour accuracy.
$hrs = (int)round($diffabs / 60 / 60);
$out = ($hrs == 1) ? sprintf($str['hour'], $hrs) : sprintf($str['hours'], $hrs);
} elseif ($diffabs >= 86400 && $diffabs < 604800) {
// Day accuracy.
$days = round($diffabs / 60 / 60 / 24);
$out = ($days == 1) ? sprintf($str['day'], $days) : sprintf($str['days'], $days);
}
}
if (!empty($out)) {
if ($diff > 0) {
$out .= $str['ago'];
} elseif ($diff < 0) {
$out .= $str['fromnow'];
}
} else {
if (empty($datestyle)) {
$datestyle = (date('Y', $now) === date('Y', $time)) ? 'M d' : 'M d Y';
}
$out = date($datestyle, $time);
}
return $out;
}
/**
* Get a standardized date stamp.
*
* @return string A formatted, standardized date stamp.
*/
public function getDatestamp() {
return $this->format(static::standards);
}
}