SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard
    Join Date
    Oct 2004
    Location
    Newport Beach
    Posts
    1,761
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP: 'Posted 10 Minutes Ago' or 'Posted 8 Days Ago'

    I am building a site where you can post certain media and articles. I want to have a note that says Posted however long ago, be it minutes, hours, or days.

    How can I do this?

    I can do a now() time stamp on the posts to work with.

    Unless I should use something else.

    Thanks
    Ryan
    Upcoming Movies - Movie News. Updated Daily.
    Movie Trailers - Awesome trailer site. Nuff said.

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd really like to know how to do this too, it'd be useful knowledge for a project I'm working on.

  3. #3
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When the record is inserted:

    INSERT mytable (posttime) VALUES (NOW());

    When fetched:

    SELECT posttime, NOW() AS currtime FROM mytable;

    Use data/time function and do the math for the difference.
    Christopher

  4. #4
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perhaps you are thinking something on the order of:
    PHP Code:
    function seconds_ago_to_desc($ago) {
      if (
    $ago >= 86400) {
        return 
    floor($ago/86400).' days ago';
      } elseif (
    $ago >= 3600) {
        return 
    floor($ago/3600).' hours ago';
      } elseif (
    $ago >= 60) {
        return 
    floor($ago/60).' minutes ago';
      } else {
        return 
    $ago.' seconds ago';
      }

    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  5. #5
    SitePoint Wizard
    Join Date
    Oct 2004
    Location
    Newport Beach
    Posts
    1,761
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How do I get my $ago variable?

    I have a column titled 'date2' that has the datetime format....

    Thanks
    Ryan
    Upcoming Movies - Movie News. Updated Daily.
    Movie Trailers - Awesome trailer site. Nuff said.

  6. #6
    SitePoint Wizard
    Join Date
    Oct 2004
    Location
    Newport Beach
    Posts
    1,761
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is what I went with:

    PHP Code:
    $todaydate date("Y-m-d H:i:s");

    $ago strtotime($todaydate) - strtotime($daily['date2']);
      if (
    $ago >= 86400) { 
        
    $diff floor($ago/86400).' days ago'
      } elseif (
    $ago >= 3600) { 
        
    $diff floor($ago/3600).' hours ago'
      } elseif (
    $ago >= 60) { 
        
    $diff floor($ago/60).' minutes ago'
      } else { 
        
    $diff $ago.' seconds ago'
      }

    //then echo $diff later 
    Is there an easier way?

    Thanks
    Ryan
    Upcoming Movies - Movie News. Updated Daily.
    Movie Trailers - Awesome trailer site. Nuff said.

  7. #7
    SitePoint Wizard
    Join Date
    May 2003
    Location
    Berlin, Germany
    Posts
    1,829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks perfect to me, you should wrap the date_to_second functionality in a function (better in a class), as it is likely to be reused.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •