1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Win\Calendar; |
4
|
|
|
|
5
|
|
|
use \DateTime as DateTimePHP; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Data e Hora |
9
|
|
|
*/ |
10
|
|
|
class DateTime extends DateTimePHP { |
11
|
|
|
|
12
|
|
|
const BR_DATE = 'd/m/Y'; |
13
|
|
|
const BR_DATE_TIME = 'd/m/Y H:i:s'; |
14
|
|
|
const SQL_DATE = 'Y-m-d'; |
15
|
|
|
const SQL_DATE_TIME = 'Y-m-d H:i:s'; |
16
|
|
|
const ZERO_DATE_TIME = '00/00/0000 00:00:00'; |
17
|
|
|
|
18
|
|
|
/** @return boolean */ |
19
|
|
|
public function isEmpty() { |
20
|
|
|
return (boolean) ($this->format('y') < 0); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** @return string|false */ |
24
|
|
|
public function getMonthName() { |
25
|
|
|
return Month::create($this->format('m'))->getName(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** @return string|false */ |
29
|
|
|
public function getMonthAbbre() { |
30
|
|
|
return Month::create($this->format('m'))->getNameAbbre(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** @return int */ |
34
|
|
|
public function getAge($date = 'NOW') { |
35
|
|
|
return (int) $this->diff($date)->format('%y'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** @return string */ |
39
|
|
|
public function toHtml() { |
40
|
|
|
return $this->format(static::BR_DATE_TIME); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** @return string */ |
44
|
|
|
public function __toString() { |
45
|
|
|
return $this->format(static::BR_DATE); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** @return string */ |
49
|
|
|
public function toSql() { |
50
|
|
|
return $this->format(static::SQL_DATE_TIME); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Retorna TRUE se a data é valida |
55
|
|
|
* @param string $time |
56
|
|
|
* @param string|null $format |
57
|
|
|
* @return boolean |
58
|
|
|
*/ |
59
|
|
|
public static function isValid($time, $format = null) { |
60
|
|
|
return (static::createFromFormat(!is_null($format) ? $format : static::BR_DATE_TIME, $time) !== false); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $format |
65
|
|
|
* @param string $time |
66
|
|
|
* @return boolean|static |
67
|
|
|
*/ |
68
|
|
|
public static function create($format, $time) { |
69
|
|
|
$dateTimePHP = parent::createFromFormat($format, $time); |
70
|
|
|
return new DateTime($dateTimePHP->format(static::SQL_DATE_TIME)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Cria uma data vazia: 00/00/0000 |
75
|
|
|
* @return static |
76
|
|
|
*/ |
77
|
|
|
public static function createEmpty() { |
78
|
|
|
return static::create(static::BR_DATE_TIME, static::ZERO_DATE_TIME); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
} |
82
|
|
|
|