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 :
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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/php-exercises/php-date-exercise-10.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics