Passed
Branch tests1.5 (af713c)
by Wanderson
01:19
created

DateTime::formatF()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
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 */
24
	public function getMonthName() {
25
		return $this->formatF('%B');
26
	}
27
28
	/** @return string */
29
	public function getMonthAbbre() {
30
		return $this->formatF('%b');
31
	}
32
33
	/**
34
	 * Retorna a data no formato utilizado por strftime
35
	 * @return string
36
	 */
37
	public function formatF($format) {
38
		return strftime($format, $this->getTimestamp());
39
	}
40
41
	/** @return int */
42
	public function getAge($date = 'NOW') {
43
		return (int) $this->diff($date)->format('%y');
44
	}
45
46
	/** @return string */
47
	public function toHtml() {
48
		return $this->format(static::BR_DATE_TIME);
49
	}
50
51
	/** @return string */
52
	public function __toString() {
53
		return $this->format(static::BR_DATE);
54
	}
55
56
	/** @return string */
57
	public function toSql() {
58
		return $this->format(static::SQL_DATE_TIME);
59
	}
60
61
	/**
62
	 * Retorna TRUE se a data é valida
63
	 * @param string $time
64
	 * @param string|null $format
65
	 * @return boolean
66
	 */
67
	public static function isValid($time, $format = null) {
68
		return (static::createFromFormat(!is_null($format) ? $format : static::BR_DATE_TIME, $time) !== false);
69
	}
70
71
	/**
72
	 * @param string $format
73
	 * @param string $time
74
	 * @return boolean|static
75
	 */
76
	public static function create($format, $time) {
77
		$dateTimePHP = parent::createFromFormat($format, $time);
78
		return new DateTime($dateTimePHP->format(static::SQL_DATE_TIME));
79
	}
80
81
	/**
82
	 * Cria uma data vazia: 00/00/0000
83
	 * @return static
84
	 */
85
	public static function createEmpty() {
86
		return static::create(static::BR_DATE_TIME, static::ZERO_DATE_TIME);
87
	}
88
89
}
90