|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Win\Models; |
|
4
|
|
|
|
|
5
|
|
|
use DateTime as DateTimePHP; |
|
6
|
|
|
use DateTimeInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Data e Hora |
|
10
|
|
|
*/ |
|
11
|
|
|
class DateTime extends DateTimePHP |
|
12
|
|
|
{ |
|
13
|
|
|
const BR_DATE = 'd/m/Y'; |
|
14
|
|
|
const BR_DATE_TIME = 'd/m/Y H:i:s'; |
|
15
|
|
|
const SQL_DATE = 'Y-m-d'; |
|
16
|
|
|
const SQL_DATE_TIME = 'Y-m-d H:i:s'; |
|
17
|
|
|
const ZERO_DATE_TIME = '00/00/0000 00:00:00'; |
|
18
|
|
|
|
|
19
|
|
|
/** @return string */ |
|
20
|
|
|
public function __toString() |
|
21
|
|
|
{ |
|
22
|
|
|
return $this->format(static::BR_DATE); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** @return bool */ |
|
26
|
|
|
public function isEmpty() |
|
27
|
|
|
{ |
|
28
|
|
|
return (bool) ($this->format('y') < 0); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** @return string */ |
|
32
|
|
|
public function getMonthName() |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->formatF('%B'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** @return string */ |
|
38
|
|
|
public function getMonthShortName() |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->formatF('%b'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Retorna a data no formato utilizado por strftime |
|
45
|
|
|
* @param string $format |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
|
|
public function formatF($format) |
|
49
|
|
|
{ |
|
50
|
|
|
return strftime($format, $this->getTimestamp()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Retorna a idade |
|
55
|
|
|
* @param DateTimeInterface $date data atual |
|
56
|
|
|
* @return int |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getAge($date = null) |
|
59
|
|
|
{ |
|
60
|
|
|
if (is_null($date)) { |
|
61
|
|
|
$date = new DateTimePHP(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return (int) $this->diff($date)->format('%y'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** @return string */ |
|
68
|
|
|
public function toHtml() |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->format(static::BR_DATE_TIME); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** @return string */ |
|
74
|
|
|
public function toSql() |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->format(static::SQL_DATE_TIME); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Retorna TRUE se a data é valida |
|
81
|
|
|
* @param string $time |
|
82
|
|
|
* @param string|null $format |
|
83
|
|
|
* @return bool |
|
84
|
|
|
*/ |
|
85
|
|
|
public static function isValid($time, $format = null) |
|
86
|
|
|
{ |
|
87
|
|
|
return false !== static::createFromFormat(!is_null($format) ? $format : static::BR_DATE_TIME, $time); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param string $format |
|
92
|
|
|
* @param string $time |
|
93
|
|
|
* @return bool|static |
|
94
|
|
|
*/ |
|
95
|
|
|
public static function create($format, $time) |
|
96
|
|
|
{ |
|
97
|
|
|
$dateTimePHP = parent::createFromFormat($format, $time); |
|
98
|
|
|
|
|
99
|
|
|
return new DateTime($dateTimePHP->format(static::SQL_DATE_TIME)); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Cria uma data vazia: 00/00/0000 |
|
104
|
|
|
* @return static |
|
105
|
|
|
*/ |
|
106
|
|
|
public static function createEmpty() |
|
107
|
|
|
{ |
|
108
|
|
|
return static::create(static::BR_DATE_TIME, static::ZERO_DATE_TIME); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|