Checking if date is weekend PHP

To check if a date is a weekend in PHP, you can use the date function to get the day of the week for a given date and then check if it is either 0 (Sunday) or 6 (Saturday).

Here is an example of how you can do this:

<?php

$date = '2022-06-15';
$dayOfWeek = date('w', strtotime($date));
if ($dayOfWeek == 0 || $dayOfWeek == 6) {
  echo 'The date is a weekend';
} else {
  echo 'The date is not a weekend';
}

This will output The date is not a weekend for the given date.

Watch a course Learn object oriented PHP

Alternatively, you can use the DateTime class to check if a date is a weekend. Here is an example of how you can do this:

<?php

$date = new DateTime('2022-06-15');
if ($date->format('N') >= 6) {
    echo 'The date is a weekend';
} else {
    echo 'The date is not a weekend';
}

This will also output The date is not a weekend for the given date.