PHP checkdate() Function (With Examples)

In this tutorial, we will learn about the PHP checkdate() function with its usage, syntax, parameters, return value, and examples. By IncludeHelp Last updated : December 31, 2023

PHP checkdate() function

The checkdate() function is used to check the valid Gregorian dates. It accepts the date and returns Boolean values (True/False) based on the date values.

Syntax

The syntax of the checkdate() function:

checkdate(month,day,year);

Parameters

The parameters of the checkdate() function:

  • month – It specifies the month in the numbers starting from 1 to 12.
  • day – It specifies the day in the numbers starting from 1 to 31.
  • year – It specifies the year in the numbers from 1 to 32767.

Return Value

It returns "TRUE" – if the date is valid, else it returns "FALSE".

PHP checkdate() Function Example

PHP code to check whether given date is valid Gregorian date.

<?php

var_dump(checkdate(12, 31, 2019)); //valid
var_dump(checkdate(12, 31, -2018)); //invalid
var_dump(checkdate(2, 29, 2019)); //invalid
var_dump(checkdate(2, 29, 2020)); //valid

//checking through conditions
if (checkdate(12, 31, 2019)) echo "Valid\n";
else echo "Invalid\n";

if (checkdate(12, 31, -2018)) echo "Valid\n";
else echo "Invalid\n";

if (checkdate(2, 29, 2019)) echo "Valid\n";
else echo "Invalid\n";

if (checkdate(2, 29, 2020)) echo "Valid\n";
else echo "Invalid\n";
?>

Output

The output of the above example is:

bool(true)
bool(false)
bool(false)
bool(true)
Valid
Invalid
Invalid
Valid

Reference: PHP checkdate() function

To understand the above example, you should have the basic knowledge of the following PHP topics:



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.