Passed
Pull Request — master (#26)
by Wanderson
02:55
created

Date::age()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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