I've been using the DateTime class in PHP because date() has the downsides of Unix Timestamp. However, neither approach detects invalid dates for months which don't have 31 days but attempt to use the 31st day.
Example Code:
try {
$date = new DateTime('02/31/2018');
$formattedDate = $date->format('Y-m-d');
} catch (Exception $e) {}
echo $formattedDate;
Example Output:
2018-03-03
Update 1: To use checkdate() I need the component parts of the date. To get that, I will need to instantiate a DateTime object with the date string as the constructor. In the case of '02/31/2018', it will convert it to '03/03/2018' when instantiated. At that point it will be too late to run the component month, day, year through checkdate(). What is the solution?
Update 2: Getting the component part of the dates is easy if I force a specific format (e.g. mm/dd/yyyy), but if I accept all formats accepted by strtotime(), I cannot perform a checkdate before running it through strtotime() -> date() or DateTime::__construct -> DateTime::format().