-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrazilTime.php
More file actions
165 lines (141 loc) · 5.28 KB
/
Copy pathBrazilTime.php
File metadata and controls
165 lines (141 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
namespace Onimla\HTML;
#require_once 'Time.class.php';
class BrazilTime extends Time {
public $date_format = 'long';
public $time_format = 'short';
public static $date_long = '{d} de {month_name} de {year}';
public static $date_medium = '{d} de {month_name}';
public static $date_short = '{dd}/{month}/{year}';
public static $time_long = '{h} h {min} min {sec} s';
public static $time_short = '{hh}h{min}min';
public static $midnight = ' à meia-noite';
public static $d_format = 'j';
public static $dd_format = 'd';
public static $month_format = 'm';
public static $year_format = 'Y';
public static $h_format = 'G';
public static $hh_format = 'H';
public static $min_format = 'i';
public static $sec_format = 's';
public static $separator = ' às ';
public function __construct($datetime) {
parent::__construct(FALSE, $datetime);
}
public function __toString() {
if ($this->length() < 1) {
$dt = new \DateTime($this->dateTime());
$date_format = 'date_' . $this->date_format;
$time_format = 'time_' . $this->time_format;
$result = str_replace(array(
'{d}',
'{dd}',
'{month}',
'{month_name}',
'{year}',
), array(
$dt->format(self::$d_format),
$dt->format(self::$dd_format),
$dt->format(self::$month_format),
htmlentities(self::month($dt->format('n'))),
$dt->format(self::$year_format),
), self::$$date_format);
if (intval($dt->format('G')) > 0) {
$result .= htmlentities(self::$separator);
$time_format = self::$$time_format;
if ($this->time_format == 'short' AND intval($dt->format('i')) === 0) {
$time_format = substr($time_format, 0, strpos($time_format, '{min}'));
}
$result .= str_replace(array(
'{h}',
'{hh}',
'{min}',
'{sec}',
), array(
$dt->format(self::$h_format),
$dt->format(self::$hh_format),
$dt->format(self::$min_format),
$dt->format(self::$sec_format),
), $time_format);
} else {
$result .= self::$midnight;
}
return $this->open() . $result . $this->close();
}
return parent::__toString();
}
public function setDateFormat($value) {
$this->date_format = $value;
}
public function getFormattedString($format, $spell = FALSE) {
$dt = new \DateTime($this->dateTime());
if (strpos($format, 'date_') === 0) {
return str_replace(array(
'{d}',
'{dd}',
'{month}',
'{month_name}',
'{year}',
), array(
$spell ? self::spellSmallNumbers($dt->format(self::$d_format)) : $dt->format(self::$d_format),
$dt->format(self::$dd_format),
$dt->format(self::$month_format),
self::month($dt->format('n')),
$dt->format(self::$year_format),
), self::$$format);
}
if (strpos($format, 'time_') === 0) {
# Se não for meia noite
if (intval($dt->format('G')) > 0) {
$time_format = self::$$format;
# Se o formato for curto e minutos igual a zero
if ($this->time_format == 'short' AND intval($dt->format('i')) === 0) {
$time_format = substr($time_format, 0, strpos($time_format, '{min}'));
}
return str_replace(array(
'{h}',
'{hh}',
'{min}',
'{sec}',
), array(
$dt->format(self::$h_format),
$dt->format(self::$hh_format),
$dt->format(self::$min_format),
$dt->format(self::$sec_format),
), $time_format);
} else {
return substr(self::$midnight, 4);
}
}
}
public static function month($index) {
$month_name = array(
1 => 'janeiro',
2 => 'fevereiro',
3 => 'março',
4 => 'abril',
5 => 'maio',
6 => 'junho',
7 => 'julho',
8 => 'agosto',
9 => 'setembro',
10 => 'outubro',
11 => 'novembro',
12 => 'dezembro',
);
return $month_name[$index];
}
/**
* @param string $number
*/
public static function spellSmallNumbers($number) {
$number = preg_replace('/[^\d]/', '', $number);
if (intval($number) < 21) {
$spelled = explode(',', 'zero,primeiro,dois,três,quatro,cinco,seis,'
. 'sete,oito,nove,dez,onze,doze,treze,quatorze,quinze,'
. 'dezesseis,dezesete,dezoito,dezenove,vinte');
return $spelled[$number];
}
return $number;
}
}