Home > Articles > Programming > PHP

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Validating a Date

checkdate(2, 29, 2000)

When you get a date—for example, from the user using an HTML form—this data must be validated. This includes checking whether the month exists and if the month has enough days. If it’s February, you might also want to find out whether it is a leap year.

<?php
 echo ‘2000 was ‘ . 
  (checkdate(2, 29, 2000) ? ‘a‘ : ‘no‘) . 
  ‘ leap year.<br />‘;
 echo ‘2100 will be ‘ . 
  (checkdate(2, 29, 2100) ? ‘a‘ : ‘no‘) . 
  ‘ leap year.‘;
?>

Validating Date Information (checkdate.php)

But PHP would not be PHP if you really had to do this on your own. The function checkdate() validates a date; you provide the month, the day, and the year as parameters.

  • + Share This
  • 🔖 Save To Your Account