php - Way to display “Yesterday”, “Today”

I currently use the following code below in my loop to display dates for groups of posts. Works great, was just wonderin

I currently use the following code below in my loop to display dates for groups of posts. Works great, was just wondering if it's possible to change the day from php code 'l'(Sunday-Saturday) to Today and Yesterday for the current posts.

I am guessing Wordpress doesn't have a built-in detection and a function will need to be created to replace the date here?

$my_date = the_date('l, F jS, Y', '<div class="clear"></div><h2>', '</h2>', FALSE); echo $my_date;

I currently use the following code below in my loop to display dates for groups of posts. Works great, was just wondering if it's possible to change the day from php code 'l'(Sunday-Saturday) to Today and Yesterday for the current posts.

I am guessing Wordpress doesn't have a built-in detection and a function will need to be created to replace the date here?

$my_date = the_date('l, F jS, Y', '<div class="clear"></div><h2>', '</h2>', FALSE); echo $my_date;
Share Improve this question asked Apr 5, 2019 at 22:55 Joe LandryJoe Landry 277 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

It took me less than a minute of asking the googles (or bing, or duck) to find this answer:

https://stackoverflow/questions/3454258/php-date-yesterday-today

The accepted answer has the code you want to use (or will give you a starting point).

(And this is not really a WordPress question...it's a PHP question. PHP questions belong on the Stack Overflow place: https://stackoverflow/ )

WordPress has built-in function to display time difference in a nicer way. And its even used almost everywhere in wp-admin ;)

This function is called human_time_diff. It takes two params: from and to (both as timestamps) and returns a string containing human readable time difference.

And there’s even a filter human_time_diff that will allow you to do your alterations to such strings, so you can make it more or less precise (for example the function can return “4 seconds ago”).

And if you want only the “today/yesterday”, then go for Ricks solution with simple comparison of dates.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745621307a4636533.html

相关推荐

  • php - Way to display “Yesterday”, “Today”

    I currently use the following code below in my loop to display dates for groups of posts. Works great, was just wonderin

    11小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信