我想以周年纪念日格式创建指定的年份

时间:2019-07-03 09:59:49

标签: javascript php jquery html

我想在21日,22日,23日,24日打印一年

假设今天echo = 21st 明年将更改为22日

假设原始日期是2012年,今天是2019年,它将显示出版的第六年

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

示例:

<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>

2 个答案:

答案 0 :(得分:0)

如果我对问题的理解正确,那么您正在寻找符合以下条件的东西:

$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

将其与您的HTML结合起来将类似于以下内容:

注意:HTML5不支持font标签,我已将其替换为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 />";

}

有关更多详细说明,请参见以下答案:

答案 1 :(得分:0)

这是PHP代码

<?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];
?>

这是HTML输出

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