What is the difference between using "o" and "Y" in the date function when extracting the year from a date string in PHP?

When extracting the year from a date string in PHP, using "o" will return the ISO-8601 year number while using "Y" will return the four-digit year number. The difference lies in how the year is formatted. If you need the year in the ISO-8601 format, use "o". If you need the year in the four-digit format, use "Y".

// Using "o" to extract the ISO-8601 year number
$date = "2022-12-31";
$year = date('o', strtotime($date));
echo $year; // Output: 2022

// Using "Y" to extract the four-digit year number
$date = "2022-12-31";
$year = date('Y', strtotime($date));
echo $year; // Output: 2022