Test Failed
Push — master ( c19dce...c10a28 )
by Jinyun
02:53
created

DayOfTheWeek::dayOfTheWeek2()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 4
nc 2
nop 3
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