How can you efficiently display only the year, month, or day from a date retrieved from a database in PHP?

When retrieving a date from a database in PHP, you can efficiently display only the year, month, or day by using the date() function along with the strtotime() function. By using these functions, you can easily format the retrieved date to display only the desired part (year, month, or day) without having to manipulate the date string manually.

// Retrieve date from database
$dateFromDatabase = "2022-05-15";

// Display only the year
echo date("Y", strtotime($dateFromDatabase));

// Display only the month
echo date("m", strtotime($dateFromDatabase));

// Display only the day
echo date("d", strtotime($dateFromDatabase));