WordPress.org

Ready to get started?Download WordPress

Forums

Conditional date: compare with today date (4 posts)

  1. pomchips
    Member
    Posted 2 years ago #

    Hi to all,

    I'm trying to display a conditional date in my listing artcile sidebar:

    If the article date = today date -> display hour.
    If the article date != today date -> display date.

    Actually, my post title is build like that: <?php the_time('d.m'); ?> - <?php the_title(); ?> My PHP

    My PHP knowledge are very poor, so I trying with this code:

    if(the_date("d.m") == the_time("d.m")) echo the_time("H:i");
    else echo the_time("d.m");

    Obviously, it doesn't work... Anyone to help me?

    Thanks,

  2. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    http://codex.wordpress.org/Function_Reference/get_the_time
    http://php.net/manual/en/function.date.php

    example (untested):

    if(date("d.m") == get_the_time("d.m")) echo get_the_time("H:i");
    else echo get_the_time("d.m");

    (will also show the time if the date is from exactly one or more years ago)

  3. pomchips
    Member
    Posted 2 years ago #

    Thanks a lot!!!!

  4. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    any details on what the code did or not did?

    where and how do you use the snippet?

    as it is, it needs to be within the loop.

    try what happens if you change the code to (if used in functions.php, you might need to add global $post; before the code):

    if(date("d.m") == get_the_time("d.m", $post->ID)) echo get_the_time("H:i", $post->ID);
    else echo get_the_time("d.m", $post->ID);

    if this all fails, please paste the full code of your tempalte into a http://pastebin.com/ - following the guidelines in http://codex.wordpress.org/Forum_Welcome#Posting_Code

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.