How can PHP handle user input for date and time values in a search form to ensure proper formatting and data validation?

When handling user input for date and time values in a search form, it is important to ensure proper formatting and data validation to prevent errors. One way to achieve this is by using PHP functions like strtotime() and date() to convert and validate the input. Additionally, regular expressions can be used to enforce a specific date or time format.

// Example code snippet for handling user input for date and time values

// Retrieve user input from the form
$userDate = $_POST['date'];
$userTime = $_POST['time'];

// Validate date format
if (strtotime($userDate) === false) {
    echo "Invalid date format. Please enter a valid date.";
}

// Validate time format
if (!preg_match("/^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/", $userTime)) {
    echo "Invalid time format. Please enter a valid time.";
}

// If both date and time formats are valid, proceed with further processing
// Additional code here...