Are there any best practices to follow when creating a contact form with date selection options in PHP?

When creating a contact form with date selection options in PHP, it is important to ensure that the date input is properly validated and sanitized to prevent any potential security vulnerabilities. One best practice is to use a date picker tool or a dropdown menu for selecting dates to ensure the input is in a valid format. Additionally, it is recommended to store the date in a standardized format (such as YYYY-MM-DD) in the database for consistency.

<?php
// Validate and sanitize date input
$date = isset($_POST['date']) ? $_POST['date'] : '';
$date = filter_var($date, FILTER_SANITIZE_STRING);

// Validate date format
if (preg_match("/^\d{4}-\d{2}-\d{2}$/", $date)) {
    // Date format is valid, proceed with further processing
    // Store date in database in standardized format (YYYY-MM-DD)
} else {
    // Date format is invalid, display error message to user
    echo "Invalid date format. Please enter a valid date (YYYY-MM-DD).";
}
?>