1 | <?php |
---|
2 | /** |
---|
3 | * Corresponde ao formato de data armazenado na configuração (d/m/Y ou Y/m/d) |
---|
4 | * @name DATE_FORMAT_LOCAL |
---|
5 | */ |
---|
6 | define('DATE_FORMAT_LOCAL', 'd/m/Y'); |
---|
7 | /** |
---|
8 | * maior número inteiro |
---|
9 | * @name DATE_FORMAT_LOCAL |
---|
10 | */ |
---|
11 | define('LONG_MAX', is_int(2147483648) ? 9223372036854775807 : 2147483647); |
---|
12 | /** |
---|
13 | * Formato de data customizado |
---|
14 | * @name DATE_FORMAT_LOCAL |
---|
15 | */ |
---|
16 | define('DATE_FORMAT_CUSTOM', 'CUSTOM'); |
---|
17 | |
---|
18 | /** |
---|
19 | * Classe para manipulação e realização de cálculos com datas |
---|
20 | * @author Marcos Pont |
---|
21 | * @author Carlos Eduardo Nogueira Gonçalves |
---|
22 | * @version 1.0 |
---|
23 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
24 | * @package Workflow |
---|
25 | * @subpackage local |
---|
26 | */ |
---|
27 | class wf_date |
---|
28 | { |
---|
29 | /** |
---|
30 | * Verifica se uma determinada data é válida |
---|
31 | * @access public |
---|
32 | * @param string $date Data a ser validada |
---|
33 | * @return boolean |
---|
34 | */ |
---|
35 | function isValid($date) { |
---|
36 | $regs = array(); |
---|
37 | if ($this->isEuroDate($date, $regs)) { |
---|
38 | list(, $day, $month, $year) = $regs; |
---|
39 | } else if ($this->isUsDate($date, $regs) || $this->isSqlDate($date, $regs)) { |
---|
40 | list(, $year, $month, $day) = $regs; |
---|
41 | } else { |
---|
42 | return FALSE; |
---|
43 | } |
---|
44 | if ($year < 0 || $year > 9999) { |
---|
45 | return FALSE; |
---|
46 | } else { |
---|
47 | return (checkdate($month, $day, $year)); |
---|
48 | } |
---|
49 | } |
---|
50 | /** |
---|
51 | * Verifica se um valor de time zone é válido |
---|
52 | * @access public |
---|
53 | * @param string $tz Valor de time zone |
---|
54 | * @return boolean |
---|
55 | */ |
---|
56 | function isValidTZ($tz) { |
---|
57 | return preg_match("/^(((\+|\-)[0-9]{2}\:[0-9]{2})|(UT|GMT|EST|EDT|CST|CDT|MST|MDT|PST|PDT)|([A-IK-Y]{1}))$/", $tz); |
---|
58 | } |
---|
59 | |
---|
60 | /** |
---|
61 | * Verifica se uma data está no formato europeu dd[/-.]mm[/-.]YYYY[ HH:mm:ss] |
---|
62 | * @param array &$regs Vetor para onde retornam os valores destacados de dia, mês e ano |
---|
63 | * @access public |
---|
64 | * @return boolean |
---|
65 | */ |
---|
66 | function isEuroDate($date, &$regs) { |
---|
67 | $date = trim($date); |
---|
68 | if (ereg("^([0-9]{1,2})(/|\-|\.)([0-9]{1,2})(/|\-|\.)([0-9]{4})([[:space:]]([0-9]{1,2}):([0-9]{1,2}):?([0-9]{1,2})?)?$", $date, $matches)) { |
---|
69 | $regs = array( |
---|
70 | $matches[0], |
---|
71 | $matches[1], $matches[3], $matches[5], |
---|
72 | $matches[7], $matches[8], $matches[9] |
---|
73 | ); |
---|
74 | return TRUE; |
---|
75 | } |
---|
76 | return FALSE; |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * Verifica se uma data está no formato americano YYYY[/-.]mm[/-.]dd[ HH:mm:ss] |
---|
81 | * @param string $date Data a ser verificada |
---|
82 | * @param array &$regs Vetor para onde retornam os valores destacados de dia, mês e ano |
---|
83 | * @access public |
---|
84 | * @return boolean |
---|
85 | */ |
---|
86 | function isUsDate($date, &$regs) { |
---|
87 | $date = trim($date); |
---|
88 | if (ereg("^([0-9]{4})(/|\-|\.)([0-9]{1,2})(/|\-|\.)([0-9]{1,2})([[:space:]]([0-9]{1,2}):([0-9]{1,2}):?([0-9]{1,2})?)?$", $date, $matches)) { |
---|
89 | $regs = array( |
---|
90 | $matches[0], |
---|
91 | $matches[1], $matches[3], $matches[5], |
---|
92 | $matches[7], $matches[8], $matches[9] |
---|
93 | ); |
---|
94 | return TRUE; |
---|
95 | } |
---|
96 | return FALSE; |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * Verifica se uma data está no formato SQL YYYY-mm-dd |
---|
101 | * @param string $date Data a ser verificada |
---|
102 | * @param array &$regs Vetor para onde retornam os valores destacados de dia, mês e ano |
---|
103 | * @access public |
---|
104 | * @return boolean |
---|
105 | */ |
---|
106 | function isSqlDate($date, &$regs) { |
---|
107 | $date = trim($date); |
---|
108 | if (ereg("^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})([[:space:]]([0-9]{1,2}):([0-9]{1,2}):?([0-9]{1,2})?)?$", $date, $matches)) { |
---|
109 | $regs = array( |
---|
110 | $matches[0], |
---|
111 | $matches[1], $matches[2], $matches[3], |
---|
112 | $matches[5], $matches[6], $matches[7] |
---|
113 | ); |
---|
114 | return TRUE; |
---|
115 | } |
---|
116 | return FALSE; |
---|
117 | } |
---|
118 | |
---|
119 | /** |
---|
120 | * Verifica se uma data é posterior à data atual |
---|
121 | * @access public |
---|
122 | * @param string $date Data a ser verificada |
---|
123 | * @return boolean |
---|
124 | */ |
---|
125 | function isFuture($date) { |
---|
126 | $daysFrom = $this->dateToDays($date); |
---|
127 | $daysTo = $this->dateToDays(); |
---|
128 | return ($daysFrom > $daysTo); |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | * Verifica se uma data é anterior à data atual |
---|
133 | * @access public |
---|
134 | * @param string date Data a ser verificada |
---|
135 | * @return boolean |
---|
136 | */ |
---|
137 | function isPast($date) { |
---|
138 | $daysFrom = $this->dateToDays($date); |
---|
139 | $daysTo = $this->dateToDays(); |
---|
140 | return ($daysTo > $daysFrom); |
---|
141 | } |
---|
142 | |
---|
143 | /** |
---|
144 | * Calcula o dia seguinte em relação à data atual |
---|
145 | * @access public |
---|
146 | * @return string Data calculada |
---|
147 | */ |
---|
148 | function tomorrow() { |
---|
149 | return $this->nextDay(); |
---|
150 | } |
---|
151 | |
---|
152 | /** |
---|
153 | * Verifica se um determinado valor é NULL |
---|
154 | * @param mixed $value Valor a testar |
---|
155 | * @param boolean $strict Considerar o tipo do dado |
---|
156 | * @access public |
---|
157 | * @return boolean |
---|
158 | */ |
---|
159 | function isNull($value, $strict = FALSE) { |
---|
160 | return ($strict) ? (NULL === $value) : (NULL == $value); |
---|
161 | } |
---|
162 | |
---|
163 | /** |
---|
164 | * Calcula a data imediatamente posterior a uma determinada data |
---|
165 | * @access public |
---|
166 | * @param string $date Data base |
---|
167 | * @return string Dia seguinte calculado |
---|
168 | */ |
---|
169 | function nextDay($date=NULL) { |
---|
170 | if ($this->isNull($date)) { |
---|
171 | $date = $this->localDate(); |
---|
172 | } |
---|
173 | return $this->futureDate($date, 1); |
---|
174 | } |
---|
175 | |
---|
176 | /** |
---|
177 | * Calcula uma data no futuro, a partir de um número de dias, meses e anos |
---|
178 | * @access public |
---|
179 | * @param string $date Data original |
---|
180 | * @param iny $days Número de dias no futuro |
---|
181 | * @param int $months Número de meses no futuro |
---|
182 | * @param int $years Número de anos no futuro |
---|
183 | * @return string Data calculada no formato original |
---|
184 | */ |
---|
185 | function futureDate($date, $days = 0, $months = 0, $years = 0) { |
---|
186 | // Captura o formato e os elementos da data base |
---|
187 | $regs = array(); |
---|
188 | if ($this->isEuroDate($date, $regs)) { |
---|
189 | list(, $day, $month, $year) = $regs; |
---|
190 | $dateFormat = "EURO"; |
---|
191 | } else if ($this->isSqlDate($date, $regs)) { |
---|
192 | list(, $year, $month, $day) = $regs; |
---|
193 | $dateFormat = "SQL"; |
---|
194 | } else if ($this->isUsDate($date, $regs)) { |
---|
195 | list(, $year, $month, $day) = $regs; |
---|
196 | $dateFormat = "US"; |
---|
197 | } else { |
---|
198 | return NULL; |
---|
199 | } |
---|
200 | // Calcula o número de dias da data original |
---|
201 | $daysFrom = $this->dateToDays($date); |
---|
202 | $daysInc = 0; |
---|
203 | // Adiciona os anos |
---|
204 | $years = intval($years); |
---|
205 | for ($i = 1; $i <= $years; $i++) { |
---|
206 | $year++; |
---|
207 | $daysInc += ($this->isLeapYear($year)) ? 366 : 365; |
---|
208 | } |
---|
209 | // Adiciona os meses de acordo com o número de dias em cada um |
---|
210 | $months = intval($months); |
---|
211 | for ($i = 1; $i <= $months; $i++) { |
---|
212 | $mTemp = $i % 12 - 1; |
---|
213 | $yTemp = intval($i / 12); |
---|
214 | if (($month + $mTemp) > 12) { |
---|
215 | $yTemp++; |
---|
216 | $mTemp = ($month + $mTemp) - 12; |
---|
217 | } else { |
---|
218 | $mTemp = $month + $mTemp; |
---|
219 | } |
---|
220 | $daysInc += $this->daysInMonth($mTemp, $year + $yTemp); |
---|
221 | } |
---|
222 | // Adiciona os dias |
---|
223 | $daysInc += intval($days); |
---|
224 | // Retorna a data calculada no formato original |
---|
225 | return $this->daysToDate($daysFrom + $daysInc, $dateFormat); |
---|
226 | } |
---|
227 | |
---|
228 | /** |
---|
229 | * Calcula o dia anterior em relação à data atual |
---|
230 | * @access public |
---|
231 | * @return string Data calculada |
---|
232 | */ |
---|
233 | function yesterday() { |
---|
234 | return $this->prevDay(); |
---|
235 | } |
---|
236 | |
---|
237 | /** |
---|
238 | * Calcula a data imediatamente anterior a uma determinada data |
---|
239 | * @access public |
---|
240 | * @param string $date Data base |
---|
241 | * @return string Dia anterior calculado |
---|
242 | */ |
---|
243 | function prevDay($date=NULL) { |
---|
244 | if ($this->isNull($date)) { |
---|
245 | $date = $this->localDate(); |
---|
246 | } |
---|
247 | return $this->pastDate($date, 1); |
---|
248 | } |
---|
249 | |
---|
250 | /** |
---|
251 | * Calcula uma data no passado, a partir de um número de dias, meses e anos |
---|
252 | * @param string $date Data original |
---|
253 | * @param int $days Número de dias no passado |
---|
254 | * @param int $months Número de meses no passado |
---|
255 | * @param int $years Número de anos no passado |
---|
256 | * @return string Data calculada no formato original |
---|
257 | * @access public |
---|
258 | */ |
---|
259 | function pastDate($date, $days=0, $months=0, $years=0) { |
---|
260 | // Captura o formato e os elementos da data base |
---|
261 | $regs = array(); |
---|
262 | if ($this->isEuroDate($date, $regs)) { |
---|
263 | list(, $day, $month, $year) = $regs; |
---|
264 | $dateFormat = 'EURO'; |
---|
265 | } else if ($this->isSqlDate($date, $regs)) { |
---|
266 | list(, $year, $month, $day) = $regs; |
---|
267 | $dateFormat = 'SQL'; |
---|
268 | } else if ($this->isUsDate($date, $regs)) { |
---|
269 | list(, $year, $month, $day) = $regs; |
---|
270 | $dateFormat = 'US'; |
---|
271 | } else { |
---|
272 | return NULL; |
---|
273 | } |
---|
274 | // Calcula o número de dias da data original |
---|
275 | $daysFrom = $this->dateToDays($date); |
---|
276 | $daysDec = 0; |
---|
277 | // Adiciona os anos |
---|
278 | for ($i = 1; $i <= $years; $i++) { |
---|
279 | $s = ($this->isLeapYear($year)) ? 366 : 365; |
---|
280 | $daysDec += ($this->isLeapYear($year)) ? 366 : 365; |
---|
281 | $year--; |
---|
282 | } |
---|
283 | // Adiciona os meses de acordo com os dias de cada mês |
---|
284 | for ($i = 1; $i <= $months; $i++) { |
---|
285 | $mTemp = $i % 12; |
---|
286 | $yTemp = intval($i / 12); |
---|
287 | if (($month - $mTemp) <= 0) { |
---|
288 | $yTemp++; |
---|
289 | $mTemp = 12 + ($month - $mTemp); |
---|
290 | } else { |
---|
291 | $mTemp = $month - $mTemp; |
---|
292 | } |
---|
293 | $daysDec += $this->daysInMonth($mTemp, $year - $yTemp); |
---|
294 | } |
---|
295 | // Adiciona os dias |
---|
296 | $daysDec += $days; |
---|
297 | // Retorna a data calculada no formato original |
---|
298 | return $this->daysToDate($daysFrom - $daysDec, $dateFormat); |
---|
299 | } |
---|
300 | |
---|
301 | /** |
---|
302 | * Calcula a diferença em dias entre duas datas |
---|
303 | * @param string $dateM Data 1 |
---|
304 | * @param string $dateS Data 2 |
---|
305 | * @param boolean $unsigned Usar sinal no retorno |
---|
306 | * @access public |
---|
307 | */ |
---|
308 | function getDiff($dateM, $dateS, $unsigned=TRUE) { |
---|
309 | // Calcula o número de dias da diferença |
---|
310 | $daysS = $this->dateToDays($dateS); |
---|
311 | $daysM = $this->dateToDays($dateM); |
---|
312 | return ($unsigned? abs($daysS - $daysM) : ($daysS - $daysM)); |
---|
313 | } |
---|
314 | |
---|
315 | /** |
---|
316 | * Retorna o dia da semana para uma data |
---|
317 | * @param string $date Data a ser processada |
---|
318 | * @param boolean $text Retornar o nome do dia da semana |
---|
319 | * @param boolean $abbr Retornar o nome do dia da semana abreviadamente |
---|
320 | * @return mixed Nome ou número do dia da semana (baseado em zero) |
---|
321 | * @access public |
---|
322 | */ |
---|
323 | function dayOfWeek($date, $text=TRUE, $abbr=FALSE) { |
---|
324 | // Captura os elementos da data base de acordo com o formato |
---|
325 | $regs = array(); |
---|
326 | if ($this->isEuroDate($date, $regs)) { |
---|
327 | list(, $day, $month, $year) = $regs; |
---|
328 | } else if ($this->isUsDate($date, $regs) || $this->isSqlDate($date, $regs)) { |
---|
329 | list(, $year, $month, $day) = $regs; |
---|
330 | } else { |
---|
331 | return NULL; |
---|
332 | } |
---|
333 | // Cálculo do dia da semana |
---|
334 | if ($month > 2) { |
---|
335 | $month -= 2; |
---|
336 | } else { |
---|
337 | $month += 10; |
---|
338 | $year--; |
---|
339 | } |
---|
340 | $dow = (floor((13 * $month - 1) / 5) + $day + ($year % 100) + floor(($year % 100) / 4) + floor(($year / 100) / 4) - 2 * floor($year / 100) + 77); |
---|
341 | $dow = (($dow - 7 * floor($dow / 7))); |
---|
342 | // Exibição do resultado, de acordo com os parâmetros fornecidos |
---|
343 | if ($text) { |
---|
344 | $daysOfWeek = array('segunda', 'terça', 'quarta', 'quinta', 'sexta'); |
---|
345 | if ($abbr) { |
---|
346 | return $daysOfWeek[$dow]; |
---|
347 | } else { |
---|
348 | return $daysOfWeek[$dow] . '-feira'; |
---|
349 | } |
---|
350 | } else { |
---|
351 | return $dow; |
---|
352 | } |
---|
353 | } |
---|
354 | |
---|
355 | /** |
---|
356 | * Retorna o número de dias de um mês de acordo com o mês e o ano |
---|
357 | * @param int $month Mês |
---|
358 | * @param int $year Ano |
---|
359 | * @return int Número de dias do mês solicitado |
---|
360 | * @access public |
---|
361 | */ |
---|
362 | function daysInMonth($month=NULL, $year=NULL) { |
---|
363 | if ($this->isNull($year)) |
---|
364 | $year = date("Y"); |
---|
365 | if ($this->isNull($month)) |
---|
366 | $month = date("m"); |
---|
367 | if ($month == 2) { |
---|
368 | return ($this->isLeapYear($year) ? 29 : 28); |
---|
369 | } elseif (in_array($month, array(4, 6, 9, 11))) { |
---|
370 | return 30; |
---|
371 | } else { |
---|
372 | return 31; |
---|
373 | } |
---|
374 | } |
---|
375 | |
---|
376 | /** |
---|
377 | * Verifica se um determinado ano é bissexto |
---|
378 | * @param int $year Ano para ser verificado |
---|
379 | * @access public |
---|
380 | * @return boolean |
---|
381 | */ |
---|
382 | function isLeapYear($year=NULL) { |
---|
383 | if ($this->isNull($year)) |
---|
384 | $year = date("Y"); |
---|
385 | if (strlen($year) != 4 || preg_match("/\D/", $year)) |
---|
386 | return NULL; |
---|
387 | return ((($year % 4) == 0 && ($year % 100) != 0) || ($year % 400) == 0); |
---|
388 | } |
---|
389 | |
---|
390 | /** |
---|
391 | * Converte uma data no padrão europeu (dd/mm/YYYY) para o padrão SQL (YYYY-mm-dd) |
---|
392 | * @param string $date Data a ser convertida |
---|
393 | * @param boolean @preserveTime Preservar porção de hora, se ela existir na data fornecida |
---|
394 | * @return string Data convertida ou a original se o padrão de entrada estiver incorreto |
---|
395 | * @access public |
---|
396 | */ |
---|
397 | function fromEuroToSqlDate($date, $preserveTime=FALSE) { |
---|
398 | $regs = array(); |
---|
399 | if ($this->isEuroDate($date, $regs)) { |
---|
400 | $res = "$regs[3]-$regs[2]-$regs[1]"; |
---|
401 | if ($preserveTime && $regs[4] !== FALSE && $regs[5] !== FALSE) { |
---|
402 | $res .= " $regs[4]:$regs[5]"; |
---|
403 | if ($regs[6] !== FALSE) |
---|
404 | $res .= ":$regs[6]"; |
---|
405 | } |
---|
406 | return $res; |
---|
407 | } else { |
---|
408 | return $date; |
---|
409 | } |
---|
410 | } |
---|
411 | |
---|
412 | /** |
---|
413 | * Converte uma data no padrão europeu (dd/mm/YYYY) |
---|
414 | * @param string $date Data a ser convertida |
---|
415 | * @param boolean $preserveTime Preservar porção de hora, se ela existir na data fornecida |
---|
416 | * @return string Data convertida ou a original se o padrão de entrada estiver incorreto |
---|
417 | * @access public |
---|
418 | */ |
---|
419 | function fromEuroToUsDate($date, $preserveTime=FALSE) { |
---|
420 | $regs = array(); |
---|
421 | if ($this->isEuroDate($date, $regs)) { |
---|
422 | $res = "$regs[3]/$regs[2]/$regs[1]"; |
---|
423 | if ($preserveTime && $regs[4] !== FALSE && $regs[5] !== FALSE) { |
---|
424 | $res .= " $regs[4]:$regs[5]"; |
---|
425 | if ($regs[6] !== FALSE) |
---|
426 | $res .= ":$regs[6]"; |
---|
427 | } |
---|
428 | return $res; |
---|
429 | } else { |
---|
430 | return $date; |
---|
431 | } |
---|
432 | } |
---|
433 | |
---|
434 | /** |
---|
435 | * Converte uma data no padrã americano (YYYY/mm/dd) para o padrão SQL (YYYY-mm-dd) |
---|
436 | * @param string $date Data a ser convertida |
---|
437 | * @return string Data convertida ou a original se o padrão de entrada estiver incorreto |
---|
438 | * @access public |
---|
439 | */ |
---|
440 | function fromUsToSqlDate($date) { |
---|
441 | $regs = array(); |
---|
442 | if ($this->isUsDate($date, $regs)) { |
---|
443 | return str_replace("/", "-", $date); |
---|
444 | } else { |
---|
445 | return $date; |
---|
446 | } |
---|
447 | } |
---|
448 | |
---|
449 | /** |
---|
450 | * Converte uma data no padrão americano (YYYY/mm/dd) para o padrão europeu (dd/mm/YYYY) |
---|
451 | * @param string $date Data a ser convertida |
---|
452 | * @param boolean $preserveTime Preservar porção de hora, se ela existir na data fornecida |
---|
453 | * @return string Data convertida ou a original se o padrão de entrada estiver incorreto |
---|
454 | * @access public |
---|
455 | */ |
---|
456 | function fromUsToEuroDate($date, $preserveTime=FALSE) { |
---|
457 | $regs = array(); |
---|
458 | if ($this->isUsDate($date, $regs)) { |
---|
459 | $res = "$regs[3]/$regs[2]/$regs[1]"; |
---|
460 | if ($preserveTime && $regs[4] !== FALSE && $regs[5] !== FALSE) { |
---|
461 | $res .= " $regs[4]:$regs[5]"; |
---|
462 | if ($regs[6] !== FALSE) |
---|
463 | $res .= ":$regs[6]"; |
---|
464 | } |
---|
465 | return $res; |
---|
466 | } else { |
---|
467 | return $date; |
---|
468 | } |
---|
469 | } |
---|
470 | |
---|
471 | /** |
---|
472 | * Converte uma data no padrão SQL (YYYY-mm-dd) para o padrão europeu (dd/mm/YYYY) |
---|
473 | * @param string $date Data a ser convertida |
---|
474 | * @param boolean preserveTime Preservar porção de hora, se ela existir na data fornecida |
---|
475 | * @return string Data convertida ou a original se o padrão de entrada estiver incorreto |
---|
476 | * @access public |
---|
477 | */ |
---|
478 | function fromSqlToEuroDate($date, $preserveTime=FALSE) { |
---|
479 | $regs = array(); |
---|
480 | if ($this->isSqlDate($date, $regs)) { |
---|
481 | $res = "$regs[3]/$regs[2]/$regs[1]"; |
---|
482 | if ($preserveTime && $regs[4] !== FALSE && $regs[5] !== FALSE) { |
---|
483 | $res .= " $regs[4]:$regs[5]"; |
---|
484 | if ($regs[6] !== FALSE) |
---|
485 | $res .= ":$regs[6]"; |
---|
486 | } |
---|
487 | return $res; |
---|
488 | } else { |
---|
489 | return $date; |
---|
490 | } |
---|
491 | } |
---|
492 | |
---|
493 | /** |
---|
494 | * Converte uma data no padrão SQL (YYYY-mm-dd) para o padrão americano (YYYY/mm/dd) |
---|
495 | * @param string $date Data a ser convertida |
---|
496 | * @return string Data convertida ou a original se o padrão de entrada estiver incorreto |
---|
497 | * @access public |
---|
498 | */ |
---|
499 | function fromSqlToUsDate($date) { |
---|
500 | $regs = array(); |
---|
501 | if ($this->isSqlDate($date, $regs)) { |
---|
502 | return str_replace("-", "/", $date); |
---|
503 | } else { |
---|
504 | return $date; |
---|
505 | } |
---|
506 | } |
---|
507 | |
---|
508 | /** |
---|
509 | * Converte um timestamp Unix em uma data/hora no formato DOS com 4 bytes |
---|
510 | * @param int $unixTimestamp Timestamp UNIX para a conversão |
---|
511 | * @return string Data e hora no formato DOS |
---|
512 | * @access public |
---|
513 | */ |
---|
514 | function fromUnixToDosDate($unixTimestamp=0) { |
---|
515 | $timeData = ($unixTimestamp) ? getdate($unixTimestamp) : getdate(); |
---|
516 | if ($timeData['year'] < 1980) { |
---|
517 | $timeData['year'] = 1980; |
---|
518 | $timeData['mon'] = 1; |
---|
519 | $timeData['mday'] = 1; |
---|
520 | $timeData['hours'] = 0; |
---|
521 | $timeData['minutes'] = 0; |
---|
522 | $timeData['seconds'] = 0; |
---|
523 | } |
---|
524 | return ((($timeData['year'] - 1980) << 25) | |
---|
525 | ($timeData['mon'] << 21) | |
---|
526 | ($timeData['mday'] << 16) | |
---|
527 | ($timeData['hours'] << 11) | |
---|
528 | ($timeData['minutes'] << 5) | |
---|
529 | ($timeData['seconds'] << 1)); |
---|
530 | } |
---|
531 | |
---|
532 | /** |
---|
533 | * Converte uma data nos formatos EURO, US ou SQL em um timestamp UNIX |
---|
534 | * @param string $date Data |
---|
535 | * @return int |
---|
536 | * @access public |
---|
537 | */ |
---|
538 | function dateToTime($date) { |
---|
539 | $date = $this->fromEuroToUsDate($date, TRUE); |
---|
540 | return strtotime($date); |
---|
541 | } |
---|
542 | |
---|
543 | /** |
---|
544 | * Converte uma data para o correspondente em número de dias |
---|
545 | * @param string $date Data base para o cálculo |
---|
546 | * @return int Data convertida em número de dias |
---|
547 | * @access public |
---|
548 | */ |
---|
549 | function dateToDays($date=NULL) { |
---|
550 | if ($this->isNull($date)) |
---|
551 | $date = $this->localDate(); |
---|
552 | $regs = array(); |
---|
553 | if ($this->isEuroDate($date, $regs)) { |
---|
554 | list(, $day, $month, $year) = $regs; |
---|
555 | } else if ($this->isUsDate($date, $regs) || $this->isSqlDate($date, $regs)) { |
---|
556 | list(, $year, $month, $day) = $regs; |
---|
557 | } else { |
---|
558 | return -1; |
---|
559 | } |
---|
560 | $century = (int) substr($year,0,2); |
---|
561 | $year = (int) substr($year,2,2); |
---|
562 | if ($month > 2) { |
---|
563 | $month -= 3; |
---|
564 | } else { |
---|
565 | $month += 9; |
---|
566 | if ($year) { |
---|
567 | $year--; |
---|
568 | } else { |
---|
569 | $year = 99; |
---|
570 | $century --; |
---|
571 | } |
---|
572 | } |
---|
573 | return (floor((146097 * $century) / 4 ) + floor(( 1461 * $year) / 4 ) + floor(( 153 * $month + 2) / 5 ) + $day + 1721119); |
---|
574 | } |
---|
575 | |
---|
576 | /** |
---|
577 | * Converte um número de dias em uma data |
---|
578 | * @param int $days Número de dias |
---|
579 | * @param string $dateType Tipo da data a ser retornada (EURO, US ou SQL) |
---|
580 | * @return string Data correspondente |
---|
581 | * @access public |
---|
582 | */ |
---|
583 | function daysToDate($days, $dateType) { |
---|
584 | if (!is_float($days) || !in_array(strtolower($dateType), array('euro', 'us', 'sql'))) { |
---|
585 | return NULL; |
---|
586 | } |
---|
587 | $days -= 1721119; |
---|
588 | $century = floor(( 4 * $days - 1) / 146097); |
---|
589 | $days = floor(4 * $days - 1 - 146097 * $century); |
---|
590 | $day = floor($days / 4); |
---|
591 | $year = floor(( 4 * $day + 3) / 1461); |
---|
592 | $day = floor(4 * $day + 3 - 1461 * $year); |
---|
593 | $day = floor(($day + 4) / 4); |
---|
594 | $month = floor(( 5 * $day - 3) / 153); |
---|
595 | $day = floor(5 * $day - 3 - 153 * $month); |
---|
596 | $day = floor(($day + 5) / 5); |
---|
597 | if ($month < 10) { |
---|
598 | $month +=3; |
---|
599 | } else { |
---|
600 | $month -=9; |
---|
601 | if ($year++ == 99) { |
---|
602 | $year = 0; |
---|
603 | $century++; |
---|
604 | } |
---|
605 | } |
---|
606 | $century = sprintf('%02d', $century); |
---|
607 | $year = sprintf('%02d', $year); |
---|
608 | $month = sprintf('%02d', $month); |
---|
609 | $day = sprintf('%02d', $day); |
---|
610 | if (strtolower($dateType) == 'euro') { |
---|
611 | return ("$day/$month/$century$year"); |
---|
612 | } else if (strtolower($dateType) == 'us') { |
---|
613 | return ("$century$year/$month/$day"); |
---|
614 | } else { |
---|
615 | return ("$century$year-$month-$day"); |
---|
616 | } |
---|
617 | } |
---|
618 | |
---|
619 | /** |
---|
620 | * Retorna a data local de acordo com o formato |
---|
621 | * @access public |
---|
622 | * @param int $ts Timestamp opcional para geração da data local |
---|
623 | * @return string Data local, a partir do timestamp atual ou um determinado |
---|
624 | */ |
---|
625 | function localDate($ts=0) { |
---|
626 | $dateFormat = DATE_FORMAT_LOCAL; |
---|
627 | if ($ts > 0) { |
---|
628 | if ($dateFormat) { |
---|
629 | return date($dateFormat . ' H:i:s', $ts); |
---|
630 | } else { |
---|
631 | return date("d/m/Y H:i:s", $ts); |
---|
632 | } |
---|
633 | } else { |
---|
634 | if ($dateFormat) { |
---|
635 | return date($dateFormat); |
---|
636 | } else { |
---|
637 | return date("d/m/Y"); |
---|
638 | } |
---|
639 | } |
---|
640 | } |
---|
641 | |
---|
642 | /** |
---|
643 | * Formata um valor de data a partir dos valores de dia, mês e ano |
---|
644 | * @param int $day Valor do dia na data |
---|
645 | * @param int $month Valor do mês na data |
---|
646 | * @param int $year Valor do ano na data, com 4 dígitos |
---|
647 | * @param int $fmtType Tipo de formato de data (vide constantes da classe) |
---|
648 | * @return string Data formatada |
---|
649 | * @access public |
---|
650 | */ |
---|
651 | function formatDate($day, $month, $year, $fmtType=DATE_FORMAT_LOCAL, $fmtStr='') { |
---|
652 | $day = strval(str_repeat('0', (2 - strlen($day))) . $day); |
---|
653 | $month = strval(str_repeat('0', (2 - strlen($month))) . $month); |
---|
654 | $year = strval(str_repeat('0', (4 - strlen($year))) . $year); |
---|
655 | $tsDate = mktime(0, 0, 0, $month, $day, $year); |
---|
656 | return $this->formatTime($tsDate, $fmtType, $fmtStr); |
---|
657 | } |
---|
658 | |
---|
659 | /** |
---|
660 | * Formata um valor de unix timestamp |
---|
661 | * @access public |
---|
662 | * @param int $time Unix timestamp |
---|
663 | * @param int $fmtType Tipo de formato de data (vide constantes da classse) |
---|
664 | * @param string $fmtString Descrição do formato (quando $fmtType==DATE_FORMAT_CUSTOM) |
---|
665 | * @return string Timestamp formatado |
---|
666 | */ |
---|
667 | function formatTime($time=NULL, $fmtType=DATE_FORMAT_LOCAL, $fmtStr='') { |
---|
668 | if (empty($time)) { |
---|
669 | $time = time(); |
---|
670 | } |
---|
671 | if (!is_int($time) || $time < 0 || $time > LONG_MAX) { |
---|
672 | return $time; |
---|
673 | } |
---|
674 | if ($fmtType == DATE_FORMAT_LOCAL) { |
---|
675 | return $this->localDate($time); |
---|
676 | } elseif ($fmtType == DATE_FORMAT_CUSTOM && !empty($fmtStr)) { |
---|
677 | return date($fmtStr, $time); |
---|
678 | } else { |
---|
679 | return $time; |
---|
680 | } |
---|
681 | } |
---|
682 | } |
---|
683 | ?> |
---|