Library contents

PHP checkdate

checkdate returns a boolean after validating the supplied month, day and year.

A simple example

checkdate

  1. Start with 2024-02-30
  2. Do this Validate the date
  3. Result Invalid date
Even in the leap year 2024, February ends on the 29th.

Syntax

PHP syntax
checkdate($month, $day, $year)
Run these examples in PHP

Save one example at a time as lesson.php. Use PHP 8.2 or later.

php lesson.php

Check a delivery date entered on a form

A grocery customer chooses February 30 as a delivery date. The booking desk needs to reject the impossible date and let the customer pick another day before a driver is scheduled.

From the program[$year, $month, $day] = [2025, 2, 30]
ResultChoose a valid date
lesson.php
<?php
[$year, $month, $day] = [2025, 2, 30];
echo checkdate($month, $day, $year) ? 'Book delivery' : 'Choose a valid date';
Output
Choose a valid date

February 30 cannot be booked even though the input uses the expected year-month-day format.

Advertisements

Accept a leap-year rehearsal

The music room receives a booking request for the leap day in 2028. Validate the submitted ISO date before adding the booking to the schedule.

Given2028-02-29
Worked resultBooking date accepted

Reject a short-month overflow

A tutor tries to book the art room for the thirty-first day of April. Validate the submitted ISO date before adding the booking to the schedule.

Given2027-04-31
Worked resultAsk for a new date

What to watch for

Pass the month first. The function validates Gregorian calendar fields and expects integers.

Documentation for checkdate