w3resource

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


10. Validate Given Dates

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

For more Practice: Solve these Related Problems:

  • Write a PHP script that takes a date string as input and checks whether it is a valid date using checkdate() function.
  • Write a PHP function to validate a date entered by the user and output an error message if the date is invalid.
  • Write a PHP program to iterate through an array of date strings and return the ones that are valid.
  • Write a PHP script to combine user input validation with date parsing to ensure that only valid dates are processed.

Go to:


PREV : Display Date in "Saturday the 7th" Format.
NEXT : Time Difference in Detailed Units.

PHP Code Editor:



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

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.