Passed
Pull Request — master (#21)
by Wanderson
03:27
created

Date::isValid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 2
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Win\Common\Utils;
4
5
use DateTime;
6
7
/**
8
 * Data e Hora
9
 */
10
class Date
11
{
12
	/**
13
	 * Cra data no formato desejado
14
	 * @param string $formatFrom
15
	 * @param string $date
16
	 * @param string $formatTo
17
	 * @return string
18
	 */
19
	public static function create($formatFrom, $date, $formatTo = 'Y-m-d H:i:s')
20
	{
21
		return DateTime::createFromFormat($formatFrom, $date)->format($formatTo);
22
	}
23
24
	/**
25
	 * Formata a data
26
	 * @param string $date
27
	 * @param string $format
28
	 */
29
	public static function format($date, $format)
30
	{
31
		if ($date) {
32
			return date($format, strtotime($date));
33
		}
34
	}
35
36
	/**
37
	 * Retorna a data no formato utilizado por strftime
38
	 * @param string $dateTimePHP
39
	 * @param string $format
40
	 * @return string
41
	 */
42
	public static function formatF($date, $format)
43
	{
44
		return strftime($format, strtotime($date));
45
	}
46
47
	/**
48
	 * Retorna o nome do mês
49
	 * @param string $date
50
	 * @return string
51
	 */
52
	public static function month($date)
53
	{
54
		return static::formatF($date, '%B');
55
	}
56
57
	/**
58
	 * Retorna o nome do mês abreviado
59
	 * @param string $date
60
	 * @return string
61
	 */
62
	public static function monthAbbr($date)
63
	{
64
		return static::formatF($date, '%b');
65
	}
66
67
	/**
68
	 * Retorna a idade
69
	 * @param string $date1 Data de Nascimento
70
	 * @param string $date2
71
	 * @return int
72
	 */
73
	public static function age($date1, $date2 = 'now')
74
	{
75
		return (new DateTime($date1))->diff(new DateTime($date2))->y;
76
	}
77
78
	/**
79
	 * Retorna TRUE se a data é valida
80
	 * @param string $date
81
	 * @param string|null $format
82
	 * @return bool
83
	 */
84
	public static function isValid($date, $format = 'Y-m-d H:i:s')
85
	{
86
		$d = DateTime::createFromFormat($format, $date);
87
		return $d && $d->format($format) == $date;
88
	}
89
}
90