1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace leetcode; |
6
|
|
|
|
7
|
|
|
class DayOfTheWeek |
8
|
|
|
{ |
9
|
|
|
public static function dayOfTheWeek(int $day, int $month, int $year): string |
10
|
|
|
{ |
11
|
|
|
if (empty($day) || empty($month) || empty($year)) { |
12
|
|
|
return ''; |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
$isLeapYear = static function (int $x): bool { |
16
|
|
|
return ($x % 400 === 0) || ($x % 4 === 0 && $x % 100 !== 0); |
17
|
|
|
}; |
18
|
|
|
$days = -1; // Start from 1971-01-01, remove that day. |
19
|
|
|
$weeks = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; |
20
|
|
|
$months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; |
21
|
|
|
for ($i = 1971; $i < $year; $i++) { |
22
|
|
|
$day += $isLeapYear($i) ? 366 : 365; |
23
|
|
|
} |
24
|
|
|
$months[1] = $isLeapYear($year) ? 29 : $months[1]; |
25
|
|
|
$days += array_sum(array_slice($months, 0, $month - 1)) + $day; |
26
|
|
|
|
27
|
|
|
return $weeks[(5 + $days) % 7]; // // Because it was Friday in 1971/1/1. |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public static function dayOfTheWeek2(int $day, int $month, int $year): string |
31
|
|
|
{ |
32
|
|
|
if (empty($day) || empty($month) || empty($year)) { |
33
|
|
|
return ''; |
34
|
|
|
} |
35
|
|
|
$weeks = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; |
36
|
|
|
|
37
|
|
|
return $weeks[date('w', strtotime("{$year}-{$month}-{$day}"))]; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|