␡
- Using Text Within date()
- Automatically Localizing Dates
- Manually Localizing Dates
- Using the Current Date the U.S./U.K./European Way
- Formatting a Specific Date
- Validating a Date
- Calculating a Relative Date
- Creating a Sortable Time Stamp
- Converting a String into a Date
- Determining Sunrise and Sunset
- Using Date and Time for Benchmarks
- Using Form Fields for Date Selection
- Create Self-updating Form Fields for Date Selection
- Calculating the Difference Between Two Dates
- Using GMT Date/Time Information
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.