简体   繁体   English

如何显示今天和昨天的使用日期?

[英]How do I display Today and Yesterday using date?

I am fetching data from the database. 我正在从数据库中获取数据。 Now I have a date which is below 现在我有一个日期低于

`2019-03-10 04:50:26`
`2019-03-09 01:30:55`
`2019-03-08 16:22:45`

What I am doing is, If the date is today's date then " Today " if the date is yesterday date then display " Yesterday " else display actual date. 我正在做的是,如果日期是今天的日期,那么“ Today ”,如果日期是昨天的日期,则显示“ Yesterday ”,否则显示实际日期。

My expected output is 我的预期输出是

Today | Sunday

Yesterday | Saturday

8th March 2019 | Friday

I tried below code. 我尝试下面的代码。

$nofiDate=date('jS F Y', strtotime($result->created_added)); // getting `8th March 2019`
$day = date('l', strtotime($result->created_added)); // getting days name "Friday"

using the above code I am getting output is 8th March 2019 | Friday 使用上面的代码,我得到的输出是8th March 2019 | Friday 8th March 2019 | Friday

but how do I display today and Yesterday using date? 但是如何显示今天和昨天的使用日期?

You need to do if else comparison, if date is equal today date then assign variable $day = 'Today' else if subtracted by one day must be Yesterday This code should work 您需要进行其他比较,如果日期等于今天的日期,则分配变量$ day ='Today',否则如果减去一天则必须是昨天

    if($day==date('l')){
       $day = 'Today';
    }else if(date('l',  now() - (24 * 60 * 60))){
       $day = 'Yesterday';
    }

Convert a time today and yesterday to date strings in the same format that you're using. 将今天和昨天的时间转换为与您使用的格式相同的日期字符串。 Then compare with them, and replace with the words you want. 然后与它们进行比较,并用所需的单词替换。

$today = date("jS F Y");
$yesterday = date("jS F Y", time() - 86400); // 86400 seconds in a day

Then you can compare $nofiDate with those, and show the strings you want instead: 然后,您可以将$nofiDate与之比较,并显示所需的字符串:

if ($nofiDate == $today) {
    $nofiDate = "Today";
} elseif ($nofiDate == $yesterday) {
    $nofiDate = "Yesterday";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM