You can check if a DateTime object represents a leap year in PHP using the format()
function to extract the year from the DateTime object and then use the date()
function to check if it is a leap year.
Here's an example:
$dateTime = new DateTime('2024-01-01');
$year = $dateTime->format('Y');
if (date('L', strtotime("$year-01-01"))) {
echo "$year is a leap year.";
} else {
echo "$year is not a leap year.";
}
In this example, the format()
function is used to extract the year from the DateTime object and store it in the $year
variable. Then, the date()
function is used to check if the year is a leap year.
The strtotime()
function is used to convert the year to a valid date format (YYYY-01-01
), and the date()
function with the L
format is used to check if it returns 1
for a leap year or 0
for a non-leap year.
You can customize the DateTime object ('2024-01-01'
in this example) to check if a specific DateTime object is representing a leap year.