在 PHP 中显示带有序数后缀的日期

Mir*_*ira 5 php

我目前在我的 php 代码中显示日期,如下所示

$date = $news_row['date'];
$newDate = date("l M d Y", strtotime($date));
Run Code Online (Sandbox Code Playgroud)

它给我的输出就像

2021 年 3 月 8 日星期一

相反,我想要输出像

2021 年 3 月 8 日 星期一

我找到了功能

function ordinal($number) {
    $ends = array('th','st','nd','rd','th','th','th','th','th','th');
    if ((($number % 100) >= 11) && (($number%100) <= 13))
        return $number. 'th';
    else
        return $number. $ends[$number % 10];
}
Run Code Online (Sandbox Code Playgroud)

关于这个答案

但我不知道如何将它与我的日期格式一起使用。如果这里有人可以帮助我,请告诉我。

多谢!

inv*_*bot 5

$newDate = date('l M jS Y', strtotime($date));
Run Code Online (Sandbox Code Playgroud)

了解 PHP 中的更多日期时间格式

https://www.php.net/manual/en/datetime.format.php

l => A full textual representation of the day of the week

M=> A short textual representation of a month, three letters, 

j => Day of the month without leading zeros

S => English ordinal suffix for the day of the month, 2 characters

Y => A full numeric representation of a year, 4 digits
Run Code Online (Sandbox Code Playgroud)


Ami*_* MB 3

您可以指定任何您想要的字符:

$newDate = date("l M d\t\h Y", strtotime($date));
Run Code Online (Sandbox Code Playgroud)

但如果你想得到1st, 2nd, 3rd, 4th ...你应该使用:

$newDate = date("l M jS Y", strtotime($date));
Run Code Online (Sandbox Code Playgroud)