Passed
Pull Request — master (#27)
by Wanderson
03:15 queued 01:22
created

Date   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isValid() 0 4 2
A formatF() 0 3 1
A format() 0 4 2
A age() 0 3 1
A create() 0 4 2
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
	const FORMAT_MONTH_ABBR = '%B';
13
	const FORMAT_MONTH_NAME = '%b';
14
	
15
	/**
16
	 * Cra data no formato desejado
17
	 * @param string $formatFrom
18
	 * @param string $date
19
	 * @param string $formatTo
20
	 * @return string|null
21
	 */
22
	public static function create($formatFrom, $date, $formatTo = 'Y-m-d H:i:s')
23
	{
24
		$dateTime = DateTime::createFromFormat($formatFrom, $date);
25
		return $dateTime ? $dateTime->format($formatTo) : null;
26
	}
27
28
	/**
29
	 * Formata a data
30
	 * @param string $date
31
	 * @param string $format
32
	 */
33
	public static function format($date, $format)
34
	{
35
		if ($date) {
36
			return date($format, strtotime($date));
37
		}
38
	}
39
40
	/**
41
	 * Retorna a data no formato utilizado por strftime
42
	 * @param string $date
43
	 * @param string $format
44
	 * @return string
45
	 */
46
	public static function formatF($date, $format)
47
	{
48
		return strftime($format, strtotime($date));
49
	}
50
51
	/**
52
	 * Retorna a idade
53
	 * @param string $date1 Data de Nascimento
54
	 * @param string $date2
55
	 * @return int
56
	 */
57
	public static function age($date1, $date2 = 'now')
58
	{
59
		return (new DateTime($date1))->diff(new DateTime($date2))->y;
60
	}
61
62
	/**
63
	 * Retorna TRUE se a data é valida
64
	 * @param string $date
65
	 * @param string|null $format
66
	 * @return bool
67
	 */
68
	public static function isValid($date, $format = 'Y-m-d H:i:s')
69
	{
70
		$d = DateTime::createFromFormat($format, $date);
71
		return $d && $d->format($format) == $date;
72
	}
73
}
74