What are the potential pitfalls of using substr() to extract specific parts of a date in PHP?

Using substr() to extract specific parts of a date in PHP can be risky because it assumes a fixed format for the date string. If the format changes or varies, the substr() approach may not work correctly. Instead, it's recommended to use date functions like date() or DateTime to parse and extract specific parts of a date reliably.

$dateString = "2022-10-15";
$date = new DateTime($dateString);
$year = $date->format('Y');
$month = $date->format('m');
$day = $date->format('d');

echo "Year: $year, Month: $month, Day: $day";