WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display text "Today" if posted today. (6 posts)

  1. 95alexander95
    Member
    Posted 4 years ago #

    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'');
    }
    ?>

  2. alchymyth
    Forum Moderator
    Posted 4 years ago #

    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');
    };
    ?>
  3. 95alexander95
    Member
    Posted 4 years ago #

    Thank you so much! :)

  4. 95alexander95
    Member
    Posted 4 years ago #

    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');
    };
    ?>
  5. 95alexander95
    Member
    Posted 4 years ago #

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

  6. 95alexander95
    Member
    Posted 4 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic