How can the date format Y-m-d (e.g. 1987-02-20) be enforced using regex in PHP and what are the common issues that may arise?

To enforce the date format Y-m-d (e.g. 1987-02-20) using regex in PHP, you can use the following regular expression pattern: "/^\d{4}-\d{2}-\d{2}$/". This pattern ensures that the date string consists of four digits for the year, followed by a hyphen, two digits for the month, another hyphen, and finally two digits for the day. Common issues that may arise include not properly escaping special characters in the regex pattern and not using anchors to match the entire string.

$date = "1987-02-20";
if (preg_match("/^\d{4}-\d{2}-\d{2}$/", $date)) {
    echo "Valid date format";
} else {
    echo "Invalid date format";
}