Skip to content
PHP Logo

In what ways can the issue of displaying only the year from a date field be optimized in PHP code?

When displaying only the year from a date field in PHP, one way to optimize the code is to use the `date()` function along with the `Y` format specifier, which represents the year in a four-digit format. This ensures that only the year is displayed without any additional information.

php
$date = "2022-10-15";
$year = date("Y", strtotime($date));
echo $year;