|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Win\Common\Utils; |
|
4
|
|
|
|
|
5
|
|
|
use Win\Models\DateTime; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Cálculo de Datas |
|
9
|
|
|
*/ |
|
10
|
|
|
class Calendar |
|
11
|
|
|
{ |
|
12
|
|
|
const SECOND = 'seconds'; |
|
13
|
|
|
const MINUTE = 'minutes'; |
|
14
|
|
|
const HOUR = 'hours'; |
|
15
|
|
|
const DAY = 'days'; |
|
16
|
|
|
const WEEK = 'weeks'; |
|
17
|
|
|
const MONTH = 'months'; |
|
18
|
|
|
const YEAR = 'years'; |
|
19
|
|
|
|
|
20
|
|
|
/** @var string[] Unidades de tempo */ |
|
21
|
|
|
private static $units = [ |
|
22
|
|
|
self::SECOND => ['segundo', 'segundos', 1], |
|
23
|
|
|
self::MINUTE => ['minuto', 'minutos', 60], |
|
24
|
|
|
self::HOUR => ['hora', 'horas', 60 * 60], |
|
25
|
|
|
self::DAY => ['dia', 'dias', 24 * 60 * 60], |
|
26
|
|
|
self::WEEK => ['semana', 'semanas', 7 * 24 * 60 * 60], |
|
27
|
|
|
self::MONTH => ['mês', 'meses', 30 * 24 * 60 * 60], |
|
28
|
|
|
self::YEAR => ['ano', 'anos', 365 * 24 * 60 * 60], |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Converte segundos em outro formato/unidade |
|
33
|
|
|
* @param int $seconds |
|
34
|
|
|
* @param string $format [d,m,Y,H,i] |
|
35
|
|
|
*/ |
|
36
|
|
|
public static function secondsToFormat($seconds = 0, $format = 'i') |
|
37
|
|
|
{ |
|
38
|
|
|
$dt1 = new DateTime('@0'); |
|
39
|
|
|
$dt2 = new DateTime("@$seconds"); |
|
40
|
|
|
|
|
41
|
|
|
return $dt1->diff($dt2)->format('%' . $format); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Retorna a data aproximada no formato humano |
|
46
|
|
|
* @param DateTime|bool $date |
|
47
|
|
|
* @return string (ex: 4 horas atrás), (ex: daqui a 5 dias) |
|
48
|
|
|
*/ |
|
49
|
|
|
public static function toTimeAgo($date) |
|
50
|
|
|
{ |
|
51
|
|
|
$timeAgo = 'indisponível'; |
|
52
|
|
|
if (false !== $date) { |
|
53
|
|
|
$time = time() - strtotime($date->format(DateTime::SQL_DATE_TIME)); |
|
54
|
|
|
|
|
55
|
|
|
if (0 === $time) { |
|
56
|
|
|
$timeAgo = 'agora mesmo'; |
|
57
|
|
|
} elseif ($time > 0) { |
|
58
|
|
|
$timeAgo = static::toHighestUnit($time) . ' atrás'; |
|
59
|
|
|
} else { |
|
60
|
|
|
$timeAgo = 'daqui a ' . static::toHighestUnit($time * (-1)); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $timeAgo; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Converte a quantidade de segundos para a maior possível |
|
69
|
|
|
* @param int $seconds |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
|
|
protected static function toHighestUnit($seconds) |
|
73
|
|
|
{ |
|
74
|
|
|
$units = array_reverse(self::$units); |
|
75
|
|
|
$time = 0; |
|
76
|
|
|
foreach ($units as $unitName => $unitInfo) { |
|
77
|
|
|
if ($seconds >= $unitInfo[2]) { |
|
78
|
|
|
$timeTotal = floor($seconds / $unitInfo[2]); |
|
79
|
|
|
$isPlural = ($timeTotal > 1); |
|
80
|
|
|
$time = $timeTotal . ' ' . self::$units[$unitName][$isPlural]; |
|
81
|
|
|
break; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $time; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|