-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDayOfTheWeek.php
39 lines (32 loc) · 1.27 KB
/
DayOfTheWeek.php
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
<?php
declare(strict_types=1);
namespace leetcode;
class DayOfTheWeek
{
public static function dayOfTheWeek(int $day, int $month, int $year): string
{
if (empty($day) || empty($month) || empty($year)) {
return '';
}
$isLeapYear = static function (int $x): bool {
return ($x % 400 === 0) || ($x % 4 === 0 && $x % 100 !== 0);
};
$days = -1; // Start from 1971-01-01, remove that day.
$weeks = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
$months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
for ($i = 1971; $i < $year; $i++) {
$day += $isLeapYear($i) ? 366 : 365;
}
$months[1] = $isLeapYear($year) ? 29 : $months[1];
$days += array_sum(array_slice($months, 0, $month - 1)) + $day;
return $weeks[(5 + $days) % 7]; // // Because it was Friday in 1971/1/1.
}
public static function dayOfTheWeek2(int $day, int $month, int $year): string
{
if (empty($day) || empty($month) || empty($year)) {
return '';
}
$weeks = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
return $weeks[date('w', strtotime("{$year}-{$month}-{$day}"))];
}
}