Get which day of the week the month starts on for

2019-09-19 12:26发布

问题:

I have a function which defines what day of the week the month starts on for the current month:

public function firstDayOfMonth()
    {
        $day =  (int) date("w", strtotime(date('m').'/01/'.date('Y').' 00:00:00'));
        return $day;
    }

How would I alter the above, in a way that allows me to get the day of the week the month starts on if I give it input, for example 'June'.

回答1:

public function firstDayOfMonth($month) {
    $day =  (int) date("w", strtotime($month . ' 01,' . date('Y') . ' 00:00:00'));
    return $day;
}


回答2:

You can simplify your code to this:

$month = 'June';
$day = (int) date('w', strtotime($month));