Home  >  Article  >  Backend Development  >  Detailed explanation of the validity example of php checkdate() function checking date

Detailed explanation of the validity example of php checkdate() function checking date

怪我咯
怪我咯Original
2017-04-17 16:31:454801browse

We all know that there are 12 months in a year, 31 days in a month (or 30 days, 28 days in February, and 29 days in February in leap years), 7 days in a week, and 24 hours in a day.. ....etc. But our computer doesn't know. It can't tell whether the data is right or wrong. Therefore, it can only rely on the functions provided by the developer to execute or check. Use the checkdate() function in PHP to check the date. In this chapter, we will explain to you the "checkdate()" function.

checkdate() function syntax:

The checkdate() function checks the validity of the date. Its syntax is as follows:

 checkdate (month , day ,year )

Detailed syntax:

Returns TRUE if the given date is valid, otherwise returns FALSE.

It has three parameters: the valid value of month is 1~12; the valid value of day is the maximum number of days in the month, such as January is 31 days, February is 29 days (leap year); the valid value of year Values ​​range from 1 to 366.

checkdate() function example

We use the checkdate() function to check a correct date and an incorrect date, The code is as follows:

First check the correct date:

<?php
header("Content-type:text/html;charset=utf-8");    //设置编码

$year=2017;       //年份
$month=4;
$day=30;

var_dump(checkdate($year,$month,$day));


?>

The running result is as follows:

Detailed explanation of the validity example of php checkdate() function checking date

Next we use the checkdate() function Let's check an incorrect date. Let's look at the above example and know that April has 30 days. Now we change 30 to 31 days. The code is as follows:

<?php
header("Content-type:text/html;charset=utf-8");    //设置编码
$year=2017;       //年份
$month=4;
$day=31;

var_dump(checkdate($month,$day,$year));

?>

The running result is as follows:

Detailed explanation of the validity example of php checkdate() function checking date

You can also write the date directly in the checkdate() function

<?php
header("Content-type:text/html;charset=utf-8");    //设置编码

var_dump(checkdate(12,4,2017));

?>

The above is a simple application of the checkdate() function. In the next section, we will explain the parameter format of the date() function. Formatting options.

The above is the detailed content of Detailed explanation of the validity example of php checkdate() function checking date. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn