-3

I want to print a year in 21st, 22nd, 23rd, 24th so on

suppose today echo=21st next year would be changed to 22nd

let's say suppose original date is 2012 and today is 2019 it would show 6th year of publication

<?
Orginal date=1996
today date=fetch current year
echo= today is 22nd year";
?>

Example:

<font face = "Oswald" size ="3">21<sup><small>ST</small></sup>  Year of publication</font>

<font face = "Oswald" size ="3">21<sup><small>ST</small></sup>  Year of publication</font>
Uzair Jan
  • 3
  • 4

2 Answers2

0

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:

Ben Carey
  • 16,540
  • 19
  • 87
  • 169
  • it work but i want in this format 21ST Year of publication – Uzair Jan Jul 03 '19 at 16:02
  • Why do you want to use the deprecated font tag? The function pre-styles the text for you in the way in which you want it displayed i.e. no need for sup and small tags – Ben Carey Jul 03 '19 at 16:06
0

Here is PHP Code

<?php
function get_year_of_publication($date) {
$now = new DateTime();
$date_of_publication = new DateTime($date);
$difference = $now->diff($date_of_publication)->y;
return $difference + 1;}
function ordinalise($number, $locale = 'en_US') {
$formatter = new NumberFormatter($locale, NumberFormatter::ORDINAL);
return $formatter->format($number);}
$publication_dates = ['1999-01-01'];
foreach ($publication_dates as $date) {
$year_of_publication = get_year_of_publication($date);
$ordinalised = ordinalise($year_of_publication);}
$dt=preg_split('#(?<=\d)(?=[a-z])#i',$ordinalised);
$str_upper = $dt[1];
?>

Here is HTML Output

 <font face = "Oswald" size ="3"><?php echo $dt[0]; ?><sup><small><?php echo strtoupper($str_upper); ?></small></sup> Year of publication  </font>
Uzair Jan
  • 3
  • 4