If I have understood the question correctly, you are looking for something along these lines:
$now = new DateTime();
$date_of_publication = new DateTime('2015-03-12');
// Calculate the difference in number of years
$difference = $now->diff($date_of_publication)->y;
// Add one to the difference in years
$difference += 1;
// Now we have the difference, we can add relevant ordinal
// PHP 5.3.0+ has a nice built in function
$locale = 'en_US'; // Set this to whatever locale you are using or based on the locale of the user
$formatter = new NumberFormatter($locale, NumberFormatter::ORDINAL);
$ordinalised = $formatter->format($difference);
echo $ordinalised; // 5th
Combining this with your HTML will look something like the following:
Note: The font
tag is not supported in HTML5, I have replaced with a span
.
function get_year_of_publication($date) {
$now = new DateTime();
$date_of_publication = new DateTime($date);
// Calculate the difference in number of years
$difference = $now->diff($date_of_publication)->y;
// Add one to the difference in years and return it
return $difference + 1;
}
function ordinalise($number, $locale = 'en_US') {
$formatter = new NumberFormatter($locale, NumberFormatter::ORDINAL);
return $formatter->format($number);
}
$publication_dates = ['2015-03-12', '2016-01-08', '2019-06-07'];
foreach ($publication_dates as $date) {
$year_of_publication = get_year_of_publication($date);
$ordinalised = ordinalise($year_of_publication);
echo "<span>{$ordinalised}</span><br />";
}
See the following answers for a more detailed description: