|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Win\Calendar; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Data e Hora |
|
7
|
|
|
*/ |
|
8
|
|
|
class Date { |
|
9
|
|
|
|
|
10
|
|
|
public $year = '00'; |
|
11
|
|
|
public $month = '00'; |
|
12
|
|
|
public $day = '00'; |
|
13
|
|
|
public $hour = '00'; |
|
14
|
|
|
public $minute = '00'; |
|
15
|
|
|
public $second = '00'; |
|
16
|
|
|
static $monthNames = [0 => "00", "Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"]; |
|
17
|
|
|
|
|
18
|
|
|
/** @var int */ |
|
19
|
|
|
public static $units = [ |
|
20
|
|
|
'seconds' => 1, |
|
21
|
|
|
'minutes' => 60, |
|
22
|
|
|
'hours' => 60 * 60, |
|
23
|
|
|
'days' => 60 * 60 * 24, |
|
24
|
|
|
'weeks' => 60 * 60 * 24 * 7, |
|
25
|
|
|
'months' => 60 * 60 * 24 * 30, |
|
26
|
|
|
'years' => 60 * 60 * 24 * 365 |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Inicia a data/hora de acordo com a string informada |
|
31
|
|
|
* @param string |
|
32
|
|
|
* @example new Data('01/01/1980 18:00'); |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct($stringDate = null) { |
|
35
|
|
|
$this->initDateTime($stringDate); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/* METODOS DE ACESSO */ |
|
39
|
|
|
|
|
40
|
|
|
public function isEmpty() { |
|
41
|
|
|
return (boolean) $this->year == 0; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function getMonthName() { |
|
45
|
|
|
return self::$monthNames[(int) $this->month]; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function getMonthAbbre() { |
|
49
|
|
|
return strtoupper(substr($this->getMonthName(), 0, 3)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Inicia o objeto de acordo com a data informada |
|
54
|
|
|
* @param string $stringDate |
|
55
|
|
|
*/ |
|
56
|
|
|
public function initDateTime($stringDate = null) { |
|
57
|
|
|
if (is_null($stringDate)): |
|
58
|
|
|
$stringDate = date("Y-m-d H:i:s"); |
|
59
|
|
|
endif; |
|
60
|
|
|
$arrayDate = explode(' ', strip_tags($stringDate) . ' '); |
|
61
|
|
|
$this->initDate($arrayDate[0]); |
|
62
|
|
|
$this->initTime($arrayDate[1]); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Inicia o Dia/Mes/Ano |
|
67
|
|
|
* @param string $stringDateOnly |
|
68
|
|
|
*/ |
|
69
|
|
|
private function initDate($stringDateOnly) { |
|
70
|
|
|
if (strpos($stringDateOnly, '/') > 0) { |
|
71
|
|
|
/* FORMATO: DD/MM/AAAA */ |
|
72
|
|
|
$date = explode('/', $stringDateOnly . '///'); |
|
73
|
|
|
array_reverse($date); |
|
74
|
|
|
} else { |
|
75
|
|
|
/* FORMATO: AAAA-MM-DD */ |
|
76
|
|
|
$date = explode('-', $stringDateOnly . '---'); |
|
77
|
|
|
} |
|
78
|
|
|
$this->year = strLengthFormat((int) $date[0], 4); |
|
79
|
|
|
$this->month = strLengthFormat((int) $date[1], 2); |
|
80
|
|
|
$this->day = strLengthFormat((int) $date[2], 2); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Inicia a Hora:Minuto:Segundo |
|
85
|
|
|
* @param string $stringTimeOnly |
|
86
|
|
|
*/ |
|
87
|
|
|
private function initTime($stringTimeOnly) { |
|
88
|
|
|
/* FORMATO: HH:MM:SS */ |
|
89
|
|
|
$d2 = explode(':', $stringTimeOnly . ':::'); |
|
90
|
|
|
$this->hour = strLengthFormat((int) $d2[0], 2); |
|
91
|
|
|
$this->minute = strLengthFormat((int) $d2[1], 2); |
|
92
|
|
|
$this->second = strLengthFormat((int) $d2[2], 2); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Retorna a data no padrao HTML BRASILEIRO: "Dia/Mês/Ano Hora:Minuto:Segundo" |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
|
|
public function toHtml() { |
|
100
|
|
|
return $this->format('d/m/y h:i:s'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Retorna a data com o formato padrão 'd/m/y' |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
|
|
public function __toString() { |
|
108
|
|
|
return $this->format('d/m/y'); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Retorna a data no padrao SQL DATETIME |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
|
|
public function toSql() { |
|
116
|
|
|
return $this->format('y-m-d h:i:s'); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Retorna a data no padrao TimeAgo |
|
121
|
|
|
* @return string |
|
122
|
|
|
*/ |
|
123
|
|
|
public function toTimeAgo() { |
|
124
|
|
|
return TimeAgo::format($this); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Retorna a data de acordo com o formato escolhido. Ex: [d/m/y h:i:s] , [y-m-d] , [y-m-d h:i:s] , etc |
|
129
|
|
|
* @param string $format |
|
130
|
|
|
* @return string Data no formato desejado |
|
131
|
|
|
*/ |
|
132
|
|
|
public function format($format = 'd/m/y') { |
|
133
|
|
|
$date = null; |
|
134
|
|
|
if ($this->year != 0 and $this->month != 0 and $this->day != 0) { |
|
135
|
|
|
$a = array('d', 'm', 'y', 'h', 'i', 's'); |
|
136
|
|
|
$b = array($this->day, $this->month, $this->year, $this->hour, $this->minute, $this->second); |
|
137
|
|
|
$date = str_replace($a, $b, strtolower($format)); |
|
138
|
|
|
} |
|
139
|
|
|
return $date; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Valida a data |
|
144
|
|
|
* @return boolean retorna True se data é valida |
|
145
|
|
|
*/ |
|
146
|
|
|
public function isValid() { |
|
147
|
|
|
if ($this->second >= 60 or $this->minute >= 60 or $this->hour >= 24) { |
|
148
|
|
|
return false; |
|
149
|
|
|
} |
|
150
|
|
|
return checkdate($this->getMonth(), $this->getDay(), $this->getYear()); |
|
|
|
|
|
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Retorna true se é uma data de nascimento válida |
|
155
|
|
|
* @return boolean |
|
156
|
|
|
*/ |
|
157
|
|
|
public function isValidBirthday() { |
|
158
|
|
|
$MIN_AGE = 0; |
|
159
|
|
|
$MAX_AGE = 200; |
|
160
|
|
|
$age = date('Y') - $this->getYear(); |
|
|
|
|
|
|
161
|
|
|
if ($age < $MIN_AGE or $age > $MAX_AGE) { |
|
162
|
|
|
return false; |
|
163
|
|
|
} |
|
164
|
|
|
return $this->isValid(); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Soma a quantidade de tempo informada |
|
169
|
|
|
* @param int $time |
|
170
|
|
|
* @param string $unit [days,month,years,etc] |
|
171
|
|
|
*/ |
|
172
|
|
|
public function sumTime($time, $unit = 'days') { |
|
173
|
|
|
$newTime = strtotime('+' . $time . ' ' . $unit, strtotime($this->format('d-m-y h:i:s'))); |
|
174
|
|
|
$this->initDateTime(date('Y-m-d H:i:s', $newTime)); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Retorna o mktime da data |
|
179
|
|
|
* @return int |
|
180
|
|
|
*/ |
|
181
|
|
|
public function toMktime() { |
|
182
|
|
|
return mktime($this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Retorna a diferença em Segundos entre as duas datas |
|
187
|
|
|
* @param Date $date |
|
188
|
|
|
* @param string $unit [days,month,years,etc] |
|
189
|
|
|
* @return int diferença na unidade desejada |
|
190
|
|
|
*/ |
|
191
|
|
|
public function diff(Date $date, $unit = 'seconds') { |
|
192
|
|
|
$mkTimeDiff = $this->toMktime() - $date->toMktime(); |
|
193
|
|
|
return $this->convertSeconds($mkTimeDiff, $unit); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Converte segundos em outra unidade |
|
198
|
|
|
* @param int $seconds |
|
199
|
|
|
* @param string $newUnit [days,month,years,etc] |
|
200
|
|
|
*/ |
|
201
|
|
|
public static function convertSeconds($seconds = 0, $newUnit = 'minutes') { |
|
202
|
|
|
return $seconds / static::$units[$newUnit]; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
} |
|
206
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.