Skip to content

Commit c764087

Browse files
authored
Merge pull request mouredev#6918 from jetiradoro/44-countdown
mouredev#44 - php
2 parents ca027a6 + f4ecc6f commit c764087

File tree

1 file changed

+220
-0
lines changed

1 file changed

+220
-0
lines changed
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
<?php
2+
3+
class CountDown
4+
{
5+
private function checkFutureDate(DateTime $date): void
6+
{
7+
$now = new DateTime();
8+
if ($date < $now) {
9+
throw new Exception("El evento ya ha pasado!");
10+
}
11+
}
12+
13+
private function drawCountDown(DateTime $date): void
14+
{
15+
system("clear");
16+
$now = new DateTime('now', new DateTimeZone('Europe/Madrid'));
17+
$now_utc = $now->setTimezone(new DateTimeZone('UTC'));
18+
$diff = $date->diff($now_utc);
19+
$days = $diff->format("%a");
20+
$hours = $diff->format("%h");
21+
$minutes = $diff->format("%i");
22+
$seconds = $diff->format("%s");
23+
24+
$str_date = $date->format("d-m-Y H:i:s");
25+
$str_now_utc = $now_utc->format("d-m-Y H:i:s");
26+
27+
echo "TU EVENTO TENDRÁ LUGAR EL PROXIMO $str_date HORA UTC" . PHP_EOL . PHP_EOL;
28+
$this->printSuccess("==============");
29+
echo " Cuenta atrás " . PHP_EOL;
30+
$this->printSuccess("==============");
31+
echo "Días: " . $days . PHP_EOL;
32+
echo "Horas: " . $hours . PHP_EOL;
33+
echo "Minutos: " . $minutes . PHP_EOL;
34+
echo "Segundos: " . $seconds . PHP_EOL;
35+
36+
if ($str_date === $str_now_utc) {
37+
echo PHP_EOL . " ============================================================================" . PHP_EOL;
38+
$this->printSuccess(" ¡Felicidades! ES LA HORA : Comenzamos a divertirnos? .");
39+
} else {
40+
sleep(1);
41+
$this->drawCountDown($date);
42+
}
43+
}
44+
45+
private function checkYear(int $year)
46+
{
47+
if (strlen((string) $year) !== 4) {
48+
throw new Exception("El año debe estar formado por 4 dígitos");
49+
}
50+
}
51+
52+
private function checkMonth(string $month): void
53+
{
54+
$int_month = intval($month);
55+
if ($int_month < 1 || $int_month > 12) {
56+
throw new Exception("El mes debe estar entre 1 y 12");
57+
}
58+
}
59+
60+
private function checkDay(int $day, int $month): void
61+
{
62+
$meses_largos = [1, 3, 5, 7, 8, 10, 12];
63+
if ($month == 2 && $day > 29) {
64+
throw new Exception("No puedes tener un día mayor a 29 en febrero");
65+
}
66+
if (!in_array($month, $meses_largos) && $day > 31) {
67+
throw new Exception("No puedes tener un día mayor al día 31 para el mes seleccionado");
68+
} else if ($day > 30) {
69+
throw new Exception("No puedes tener un día mayor al día 30 para el mes seleccionado");
70+
}
71+
}
72+
73+
private function checkHour(int $hour): void
74+
{
75+
if ($hour < 0 || $hour > 23) {
76+
throw new Exception("La hora debe estar entre 0 y 23");
77+
}
78+
}
79+
80+
private function checkMinute(int $minute): void
81+
{
82+
if ($minute < 0 || $minute > 59) {
83+
throw new Exception("Los mintuos deben estar entre 0 y 59");
84+
}
85+
}
86+
87+
private function checkSeconds(int $seconds): void
88+
{
89+
if ($seconds < 0 || $seconds > 59) {
90+
throw new Exception("Los segundos deben estar entre 0 y 59");
91+
}
92+
}
93+
94+
95+
private function printError(string $message): void
96+
{
97+
echo "\033[0;31m$message" . PHP_EOL;
98+
echo "\033[0G\033[0m";
99+
}
100+
101+
private function printSuccess(string $message): void
102+
{
103+
echo "\033[0;32m$message" . PHP_EOL;
104+
echo "\033[0G\033[0m";
105+
}
106+
107+
private function getYear(): string
108+
{
109+
try {
110+
$year = intval(trim(readline("Dame el año del evento:")));
111+
$this->checkYear($year);
112+
return (string) $year;
113+
} catch (Exception $e) {
114+
$this->printError($e->getMessage());
115+
return $this->getYear();
116+
}
117+
}
118+
119+
private function getMonth(): string
120+
{
121+
try {
122+
echo "1. Enero" . PHP_EOL;
123+
echo "2. Febrero" . PHP_EOL;
124+
echo "3. Marzo" . PHP_EOL;
125+
echo "4. Abril" . PHP_EOL;
126+
echo "5. Mayo" . PHP_EOL;
127+
echo "6. Junio" . PHP_EOL;
128+
echo "7. Julio" . PHP_EOL;
129+
echo "8. Agosto" . PHP_EOL;
130+
echo "9. Septiembre" . PHP_EOL;
131+
echo "10. Octubre" . PHP_EOL;
132+
echo "11. Noviembre" . PHP_EOL;
133+
echo "12. Diciembre" . PHP_EOL;
134+
$month = intval(trim(readline("Dame el mes del evento:")));
135+
$month = str_pad((string) $month, 2, "0", STR_PAD_LEFT);
136+
$this->checkMonth($month);
137+
return (string) $month;
138+
} catch (Exception $e) {
139+
$this->printError($e->getMessage());
140+
return $this->getMonth();
141+
}
142+
}
143+
144+
private function getDay(int $month): string
145+
{
146+
try {
147+
$day = intval(trim(readline("Dame el día del evento:")));
148+
$this->checkDay($day, $month);
149+
return (string) $day;
150+
} catch (Exception $e) {
151+
$this->printError($e->getMessage());
152+
return $this->getDay($month);
153+
}
154+
}
155+
156+
private function getHour(): string
157+
{
158+
try {
159+
$hour = intval(trim(readline("Dame la hora del evento:")));
160+
$this->checkHour($hour);
161+
$hour = str_pad((string) $hour, 2, "0", STR_PAD_LEFT);
162+
return $hour;
163+
} catch (Exception $e) {
164+
$this->printError($e->getMessage());
165+
return $this->getHour();
166+
}
167+
}
168+
169+
170+
private function getMinute(): string
171+
{
172+
try {
173+
$minutes = intval(trim(readline("Dame el minuto del evento:")));
174+
$this->checkMinute($minutes);
175+
$minutes = str_pad((string) $minutes, 2, "0", STR_PAD_LEFT);
176+
return $minutes;
177+
} catch (Exception $e) {
178+
$this->printError($e->getMessage());
179+
return $this->getMinute();
180+
}
181+
}
182+
183+
private function getSecond(): string
184+
{
185+
try {
186+
$seconds = intval(trim(readline("Dame el segundo del evento:")));
187+
$this->checkSeconds($seconds);
188+
$seconds = str_pad((string) $seconds, 2, "0", STR_PAD_LEFT);
189+
return $seconds;
190+
} catch (Exception $e) {
191+
$this->printError($e->getMessage());
192+
return $this->getSecond();
193+
}
194+
}
195+
196+
197+
public function run()
198+
{
199+
system("clear");
200+
try {
201+
$year = $this->getYear();
202+
$month = $this->getMonth();
203+
$day = $this->getDay((int) $month);
204+
$hour = $this->getHour();
205+
$minute = $this->getMinute();
206+
$second = $this->getSecond();
207+
208+
$local_date = new DateTime($year . "-" . $month . "-" . $day . ' ' . $hour . ':' . $minute . ':' . $second, new DateTimeZone('Europe/Madrid'));
209+
$utc_date = $local_date->setTimezone(new DateTimeZone('UTC'));
210+
$this->checkFutureDate($utc_date);
211+
212+
$this->drawCountDown($utc_date);
213+
} catch (Exception $e) {
214+
$this->printError($e->getMessage());
215+
}
216+
}
217+
}
218+
219+
$countdown = new CountDown();
220+
$countdown->run();

0 commit comments

Comments
 (0)