Skip to:
Content
Pages
Categories
Search
Top
Bottom

Display a list of recent topics

  • Hey guys,

    Today I managed to display a list of topics on my website. A list of the 10 LAST created topics.

    Here’s the code:

    <!-- dit genereert een lijst met topics -->
    <div id="recentetopics">
    <h2>Recente topics</h2>
    <ul>
    <?php
    global $bbdb;
    $query="SELECT * FROM bb_topics WHERE topic_status=0 ORDER BY topic_start_time DESC LIMIT 10";
    $results=$bbdb->get_results($query);
    foreach ($results as $result) {
    echo "<li>".$result->topic_start_time." &rarr; <a href='/topic.php?id=".$result->topic_id."'>".$result->topic_title."</a></li>";
    }
    ?>
    </ul>
    </div>
    <!-- einde lijst topics -->

    The problem I’m having with this code is the layout of the timestamp.

    It’s like this:

    2010-01-25 00:42:07 → Australische firma bouwt snaarloze gitaar met touchscreen
    2010-01-24 22:35:44 → Apoplectic sluit contract met Mexicaans promo bedrijf af
    2010-01-24 22:22:45 → Nieuwe video HIM online

    How can I change that timestamp into something more nice, like “january 25” or something.

    Thanks a lot.

Viewing 16 replies - 1 through 16 (of 16 total)

  • chrishajer
    Participant

    @chrishajer

    I think instead of this:

    $result->topic_start_time

    you can do this:

    echo date ('F j', strtotime($result->topic_start_time))

    Worth a shot anyway…


    Michael
    Participant

    @michael888

    @chandersbs – Thanks for sharing this, I’ll be sure to try it out. :)

    Would you possibly want it to have the date displayed in this format: x seconds/minutes/hours/days ago ?

    thanks for the help chrishajer, but that code is not working, instead it returns an error :S

    @michael if i hate the option, i’d have all items published less than 24 hours ago, show like this:

    22:35 → Apoplectic sluit contract met Mexicaans promo bedrijf af
    22:22 → Nieuwe video HIM online

    As you can see, only Hour and minutes.

    Articles older than 1 day, would be displayed as this:

    23.01 → Apoplectic sluit contract met Mexicaans promo bedrijf af
    22.01 → Nieuwe video HIM online

    As you can see only day and month.

    Sadly i can’t seem to fix how the timestamp should look. another issue i’m facing is that its showing a different time in that list than the website has, its not picking up the default local time as it should.

    To see this list in action, just visit my website at this uri:

    http://goo.gl/21i0

    At the top you will see “recent topics”


    Ben L.
    Member

    @nightgunner5

    Instead of $result->topic_start_time, you want date( strtotime( $result->topic_start_time ) < time() - 86400 ? 'F j' : 'H.i', bb_offset_time( strtotime( $result->topic_start_time ) ) )

    That should fix the date offset problem and give you the date formats you want.

    Hey Ben L.,

    Thanks, that’s what I wanted. Or actually i did describe what i wanted wrong :(

    21.42 → Australische firma bouwt snaarloze gitaar met touchscreen
    19.35 → Apoplectic sluit contract met Mexicaans promo bedrijf af
    19.22 → Nieuwe video HIM online
    16.54 → Dimmu Borgir frontman lanceert "The Wrath of Shag"
    21 Jan → The Headbangers Ball - 21 januari 2010 (GEEN AFLEVERING)
    21 Jan → DJ Dino in Fat S 29Jan2010

    The items published less than 24 hours ago, IS showing as i wrote before, but i what i didn’t think, was that it would even show items published yesterday as that time format. i was hoping that only items published TODAY would list in that time format and items older than today, would have that other time format.

    A bit my fault, i didn’t describe it properly, cos i didn’t know that this was a possibility.


    Ben L.
    Member

    @nightgunner5

    chandersbs: In that case, try date( strtotime( $result->topic_start_time ) < floor( current_time( 'timestamp' ) / 86400 ) * 86400 ? 'F j' : 'H.i', bb_offset_time( strtotime( $result->topic_start_time ) ) )

    It’s slightly different now, but still some work needed i think, see output:

    Recente topics

    21.42 → Australische firma bouwt snaarloze gitaar met touchscreen
    19.35 → Apoplectic sluit contract met Mexicaans promo bedrijf af
    19.22 → Nieuwe video HIM online
    24 Jan → Dimmu Borgir frontman lanceert "The Wrath of Shag"
    21 Jan → The Headbangers Ball - 21 januari 2010 (GEEN AFLEVERING)

    it gives the impression as if the first 3 topics where created TODAY, which is not true. it should show the day of yesterday :S

    only topics created today should have the hour:minute format :(


    kevinjohngallagher
    Member

    @kevinjohngallagher

    I’m writing this from an mobile phone so apologies if you have to double check the code:

    $post_timestamp = strtotime( $result->topic_start_time );

    $current_timestamp = time();

    $time_difference_in_seconds = $current_timestamp – $post_timestamp;

    $number_of_seconds_in_24_hours = 86400;

    if($time_difference_in_seconds < $number_of_seconds_in_24_hours)

    {

    echo date(“H:i a”t, $post_timestamp) ;

    } else {

    echo date(“jS F”, $post_timestamp) ;

    }

    Its the long winded way of writing things, but I find its slightly easier to follow what’s going on. Oh and you probably don’t need to assinn all the variables, i just did it so i didn’t have to comment the code :)


    Michael
    Participant

    @michael888

    @chandersbs: I see – well that’s also cool – it looks nice on your site. :)


    chrishajer
    Participant

    @chrishajer

    Glad I posted a completely incorrect suggestion to get the conversation going :-)

    Thanks Chris and everyone, I will test Kevin’s code tonight and see how it works.

    Keep you guys posted :-)

    Hey Kevin,

    I tried your code, it’s not working.

    @ everyone:

    I added this piece of code (".$result->topic_posts.") to show the number of replies on each topic, so now the code is like this:

    <div id="recentetopics">
    <h2>Recente topics</h2>
    <ul>
    <?php
    global $bbdb;
    $query="SELECT * FROM bb_topics WHERE topic_status=0 ORDER BY topic_start_time DESC LIMIT 10";
    $results=$bbdb->get_results($query);
    foreach ($results as $result) {
    echo "<li>".date( strtotime( $result->topic_start_time ) < floor( current_time( 'timestamp' ) / 86400 ) * 86400 ? 'd M' : 'H.i', bb_offset_time( strtotime( $result->topic_start_time ) ) )." &rarr; <a href='/topic.php?id=".$result->topic_id."'>".$result->topic_title."</a> (".$result->topic_posts.")</li>";
    }
    ?>
    </ul>
    </div>

    The output is like this:

    19.11 → Dark Mirror Ov Tragedy - The Pregnant Of Despair (2009) (4)
    19.00 → Rammstein - Live In Muenchen (2009) (2)
    18.54 → Mnemic - Sons Of The System (2010) (1)
    26 Jan → Aanraders? (2)
    25 Jan → Duitse band Scorpions stopt (2)

    I notice that topics that don’t have any replies yet, it shows the number 1 as the number of replies. It should show zero actually.

    Whadya you guys think?


    kevinjohngallagher
    Member

    @kevinjohngallagher

    Hey Kevin,

    I tried your code, it’s not working.

    which bit mate?

    Also you’ve nit asked fir how many replies, you’ve asked for how many posts (which will included the original), so just take 1 away from the number Nd you get how many replies there are :-)

    I don’t think there is an option to call the number of replies, I think posts is the only one in the database.


    kevinjohngallagher
    Member

    @kevinjohngallagher

    Exactly, so just take 1 away from the total posts and that leaves you with how many replies there are :)

    $number_of_posts = int $result->topic_post;

    $number_of_replies = $number_of_posts – 1;

Viewing 16 replies - 1 through 16 (of 16 total)
  • You must be logged in to reply to this topic.
Skip to toolbar