Total Complexity | 8 |
Total Lines | 62 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
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') |
||
72 | } |
||
73 | } |
||
74 |