WordPress.org

Ready to get started?Download WordPress

Forums

Twitter-Like Time Ago (6 posts)

  1. Connor Crosby
    Member
    Posted 3 years ago #

    Hello, I am making a wordpress theme and I would like to know how to display the time like Twitter does. For example: 1 minute ago, 5 minutes ago, 1 hour ago, and then if it has been more than 1 day ago it displays the actual date of the post (October 1st, 2010 or whatever I put for the time).

    I know I can use PHP or put it in the functions.php however I am just starting out. How can I accomplish this? I appreciate the help!

  2. JohnC28
    Member
    Posted 3 years ago #

  3. Connor Crosby
    Member
    Posted 3 years ago #

    Thanks but I am looking to create something myself, not a plugin. That plugin only displays how many --- ago and doesn't show the actual date after a certain time.

  4. JohnC28
    Member
    Posted 3 years ago #

    I am looking to create something myself, not a plugin

    Sure, but download the plugin and then pick up the code from there....

  5. Connor Crosby
    Member
    Posted 3 years ago #

    The only problem is that I am kinda new to PHP so I am not exactly sure how I can accomplish that. Thank you.

  6. JohnC28
    Member
    Posted 3 years ago #

    You need the PHP DateInterval class.

    Use it to get the difference between now and the post date (get_the_date() from WP3 onwards) and then a set of if statements to determine which format you want:

    Pseudo:

    if < 60 seconds
    n seconds ago
    if < 60 minutes
    n minutes ago
    if < 24 hours
    m hours ago
    else
    date

    Good luck.
    John

Topic Closed

This topic has been closed to new replies.

About this Topic