1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* KNUT7 K7F (https://marciozebedeu.com/) |
5
|
|
|
* KNUT7 K7F (tm) : Rapid Development Framework (https://marciozebedeu.com/). |
6
|
|
|
* |
7
|
|
|
* Licensed under The MIT License |
8
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
9
|
|
|
* Redistributions of files must retain the above copyright notice. |
10
|
|
|
* |
11
|
|
|
* @see https://github.com/knut7/framework/ for the canonical source repository |
12
|
|
|
* |
13
|
|
|
* @copyright (c) 2015. KNUT7 Software Technologies AO Inc. (https://marciozebedeu.com/) |
14
|
|
|
* @license https://marciozebedeu.com/license/new-bsd New BSD License |
15
|
|
|
* @author Marcio Zebedeu - [email protected] |
16
|
|
|
* |
17
|
|
|
* @version 1.0.2 |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Ballybran\Helpers\Time; |
21
|
|
|
|
22
|
|
|
date_default_timezone_set(DEFAULT_UTC); |
23
|
|
|
|
24
|
|
|
class Timestamp |
25
|
|
|
{ |
26
|
|
|
private static $tempo_da_sessao; |
|
|
|
|
27
|
|
|
|
28
|
|
|
public static function distanceOfTimeInWords($fromTime, $toTime = 0, $showLessThanAMinute = false) |
29
|
|
|
{ |
30
|
|
|
$distanceInSeconds = round(abs($toTime - strtotime($fromTime))); |
31
|
|
|
$distanceInMinutes = round($distanceInSeconds / 60); |
32
|
|
|
|
33
|
|
|
if ($distanceInMinutes <= 1) { |
34
|
|
|
if (!$showLessThanAMinute) { |
35
|
|
|
return ($distanceInMinutes == 0) ? 'less than a minute' : '1 minute'; |
36
|
|
|
} else { |
37
|
|
|
if ($distanceInSeconds < 5) { |
38
|
|
|
return 'less than 5 seconds'; |
39
|
|
|
} |
40
|
|
|
if ($distanceInSeconds < 10) { |
41
|
|
|
return 'less than 10 seconds'; |
42
|
|
|
} |
43
|
|
|
if ($distanceInSeconds < 20) { |
44
|
|
|
return 'less than 20 seconds'; |
45
|
|
|
} |
46
|
|
|
if ($distanceInSeconds < 40) { |
47
|
|
|
return 'about half a minute'; |
48
|
|
|
} |
49
|
|
|
if ($distanceInSeconds < 60) { |
50
|
|
|
return 'less than a minute'; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return '1 minute'; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
if ($distanceInMinutes < 45) { |
57
|
|
|
return $distanceInMinutes . ' minutes'; |
58
|
|
|
} |
59
|
|
|
if ($distanceInMinutes < 90) { |
60
|
|
|
return 'about 1 hour'; |
61
|
|
|
} |
62
|
|
|
if ($distanceInMinutes < 1440) { |
63
|
|
|
return 'about ' . round(floatval($distanceInMinutes) / 60.0) . ' hours'; |
64
|
|
|
} |
65
|
|
|
if ($distanceInMinutes < 2880) { |
66
|
|
|
return '1 day'; |
67
|
|
|
} |
68
|
|
|
if ($distanceInMinutes < 43200) { |
69
|
|
|
return 'about ' . round(floatval($distanceInMinutes) / 1440) . ' days'; |
70
|
|
|
} |
71
|
|
|
if ($distanceInMinutes < 86400) { |
72
|
|
|
return 'about 1 month'; |
73
|
|
|
} |
74
|
|
|
if ($distanceInMinutes < 525600) { |
75
|
|
|
return round(floatval($distanceInMinutes) / 43200) . ' months'; |
76
|
|
|
} |
77
|
|
|
if ($distanceInMinutes < 1051199) { |
78
|
|
|
return 'about 1 year'; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return 'over ' . round(floatval($distanceInMinutes) / 525600) . ' years'; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* currentDataTime. |
86
|
|
|
* |
87
|
|
|
* @param string $format |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public static function currentDataTime(string $format = 'Y-m-d H:i:s'): string |
92
|
|
|
{ |
93
|
|
|
$data = new \DateTime(); |
94
|
|
|
return $data->format($format); |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public static function setDataTime($data, string $strftime = '%d %B %Y', string $format = 'Y-m-d H:i:s') |
99
|
|
|
{ |
100
|
|
|
$data = new \DateTime($data); |
101
|
|
|
$data_f = $data->format($format); |
102
|
|
|
|
103
|
|
|
// setlocale() used with strftime(). |
104
|
|
|
$my_locale = setlocale(LC_ALL, MY_LOCALE); |
|
|
|
|
105
|
|
|
if (MY_LOCALE == true) { |
|
|
|
|
106
|
|
|
return $data_inicial = strftime($strftime, strtotime(trim($data_f))); |
|
|
|
|
107
|
|
|
} else { |
108
|
|
|
return $data_f = $data->format($format); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public static function nicetime($date, array $translate = ["second", "minute", "hour", "day", "week", "month", "year", "decade"]) |
113
|
|
|
{ |
114
|
|
|
if (empty($date)) { |
115
|
|
|
return "No date provided"; |
116
|
|
|
} |
117
|
|
|
if (!is_array($translate)) { |
|
|
|
|
118
|
|
|
return "the expected value is not an array"; |
119
|
|
|
} |
120
|
|
|
if (5 > count($translate)) { |
121
|
|
|
return "the matrix needs 5 to 8 values (second, minute, hour, day, week,month, year, decade)"; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$periods = $translate; |
125
|
|
|
$lengths = array("60", "60", "24", "7", "4.35", "12", "10"); |
126
|
|
|
|
127
|
|
|
$now = time(); |
128
|
|
|
$unix_date = strtotime($date); |
129
|
|
|
|
130
|
|
|
// check validity of date |
131
|
|
|
if (empty($unix_date)) { |
132
|
|
|
return "Bad date"; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
// is it future date or past date |
136
|
|
|
if ($now > $unix_date) { |
137
|
|
|
$difference = $now - $unix_date; |
138
|
|
|
$tense = "ago"; |
139
|
|
|
|
140
|
|
|
} else { |
141
|
|
|
$difference = $unix_date - $now; |
142
|
|
|
$tense = "from now"; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
for ($j = 0; $difference >= $lengths[$j] && $j < count($lengths) - 1; $j++) { |
146
|
|
|
$difference /= $lengths[$j]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$difference = round($difference); |
150
|
|
|
|
151
|
|
|
if ($difference != 1) { |
152
|
|
|
$periods[$j] .= "s"; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return "$difference $periods[$j] {$tense}"; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
} |
159
|
|
|
|