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;

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


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.com/ )

  • I found that answer and have been trying to work with it but it I can't get it to work together. – Joe Landry Apr 6 at 1:34
  • The code you supplied didn't reference your attempts at using code similar to the link in my answer. All you need to do is to grab the date of the post into a variable (in the correct format) and then use that code (slightly altered) to return the "Today" or "Yesterday" text. – Rick Hellewell Apr 6 at 2:06

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.