w3resource

PHP Date Exercises : Check whether the given dates are valid or not

PHP date: Exercise-10 with Solution

Write a PHP script to check whether the given dates are valid or not?

Sample Solution:

PHP Code:

<?php
var_dump(checkdate(2, 30, 2008)); // Checks if the given date (February 30, 2008) is valid.
var_dump(checkdate(2, 29, 2008)); // Checks if the given date (February 29, 2008) is valid.
?>

Output:

bool(false)                                                 
bool(true)

Explanation:

In the exercise above,

  • checkdate(2, 30, 2008): Checks if the date February 30, 2008 is a valid date.
  • checkdate(2, 29, 2008): Checks if the date February 29, 2008 is a valid date (2008 was a leap year, so February 29 is valid).

Flowchart :

Flowchart: Check whether a given dates is valid or not

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a PHP script to print like : Saturday the 7th
Next: Write a PHP script to get time difference in days and years, months, days, hours, minutes, seconds between two dates.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.