Check if date of post is yesterday

In a loop, I want to display “Yesterday” if the date of the post(s) are dated from yesterday…

So here it goes:

Read More
<?php
    if( date('Yz') == get_the_time('Yz') ) {
        echo 'Today';
    } elseif ( date('') == get_the_time('') ) {
        echo  'Yesterday';
    } else {
        the_date();
    };
?>

Would you guys know the correct syntax on the elseif line?

Best regards,
Alex


Well, I can’t answer my question yet because I’m a noob (not enough reputation). So here it is:

<?php $w_h = $w_d = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php
    if ( date('Yz') == get_the_time('Yz') ) {
        if (!$w_d++) echo 'Today<br />';
    } elseif ( date('Yz')-1 == get_the_time('Yz') ) {
        if (!$w_h++) echo 'Yesterday<br />';
    } else {
        echo the_date();
    };
?>

Related posts

Leave a Reply

1 comment

  1. As you discovered, one possible solution is something like:

    <?php $w_h = $w_d = 0; ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php
        if ( date('Yz') == get_the_time('Yz') ) {
            if (!$w_d++) echo 'Today<br />';
        } elseif ( date('Yz')-1 == get_the_time('Yz') ) {
            if (!$w_h++) echo 'Yesterday<br />';
        } else {
            echo the_date();
        };
    ?>