What are the potential pitfalls of not properly handling date inputs in PHP?

Improperly handling date inputs in PHP can lead to unexpected results or errors in your code. It is important to validate and sanitize user input to ensure that the date format is correct and prevent any potential security vulnerabilities.

// Example of properly handling date input in PHP
$date = $_POST['date'];

// Validate date format using DateTime class
$dateObj = DateTime::createFromFormat('Y-m-d', $date);
if (!$dateObj) {
    echo "Invalid date format";
} else {
    $formattedDate = $dateObj->format('Y-m-d');
    echo "Date: " . $formattedDate;
}