WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Display text “Today” if posted today.

[Resolved] Display text “Today” if posted today.

  • hey!

    I have a problem. I have this code to get the text “Today” to show up if the post is posted the same day. The problem is, that it displays the text today for posts that were posted less than 24 hours ago, not the same date. So how can I get this to work. I want “today” to show up if the post is posted on the same date as today.

    <?php
    $days_old = (current_time(timestamp) – get_the_time(‘U’) – (get_settings(‘gmt_offset’) *3600 ) ) / (60*60*24);
    if ($days_old <=1) {
    echo “<img src=’Today!’>”;
    } elseif ($days_old > 1 & $days_old <= 2) {
    echo ‘Yesterday!’;
    } else {
    the_time(‘F jS, Y”);
    }
    ?>

Viewing 5 replies - 1 through 5 (of 5 total)
  • Michael

    @alchymyth

    Forum Moderator

    if you don’t care about showing the ‘yesterday’ bit as well, then the code could be:

    <?php
    if( date('Yz') == get_the_time('Yz') ) {
    echo 'today';
    } else {
    the_time('F jS, Y');
    };
    ?>

    Thank you so much! 🙂

    Hmm, i live in sweden, and the time doesnt seem to be right. Its 21 minutes over 12 (in the middle of the night) and the “Today” hasnt changed. How can I fix this?

    <?php
    if( date('Yz') == get_the_time('Yz') ) {
    echo 'today';
    } else {
    the_time('F jS, Y');
    };
    ?>

    does date(‘Yz’) have something to do with that?

    Hey guys! I found the soloution. 🙂

    I went inte the source code to a file called “WP-Settings.php” and changed -> date_default_timezone_set( ‘UTC’ ); <- to date_default_timezone_set( ‘Europe/Stockholm’ ); which is my local time.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] Display text “Today” if posted today.’ is closed to new replies.
Skip to toolbar