SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: Date intervals!

  1. #1
    SitePoint Zealot backtobasics's Avatar
    Join Date
    Aug 2006
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Date intervals!

    OK, so I have a 'latest posts' script which basically gets the last 3 posted blogs on my site.

    So, for example it would show:

    My Last Blog
    By: Sean
    Sat, 17th Feb 07

    However, I wish to show how long ago the blog was actually posted. Therefore instead of showing 'Sat, 17th Feb 07' it would return '6 mins ago' or '2 days, 15 mins ago'...you get the idea.

    What would be the best method for going about this?

  2. #2
    SitePoint Enthusiast louis11's Avatar
    Join Date
    Jul 2004
    Location
    United States of America
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Get todays date, and compare it with the date of the posting. You may have to convert it all into a numeral value and subtract then convert back to a readable date.

    Take a look at the date() and strtotime() functions.

    Hope that helps

  3. #3
    SitePoint Zealot backtobasics's Avatar
    Join Date
    Aug 2006
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not really haha. I know how to do that.

    Cheers anyways

  4. #4
    SitePoint Member
    Join Date
    May 2005
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use this function for showing something's age. Definitely not the smartest code but hey it works.
    PHP Code:
    function showSmartDate($date,$date2 NULL) {
    /*
    **
    */
        
    $sec 1;
        
    $min 60;
        
    $hour 60 60;
        
    $day 60*60*24;
        
    $week $day 7;
        
    $month $day 30;
        
    $year $month 12;
        if (
    $date2 == NULL)
            
    $date2 time();

        
    $diff abs($date2 $date);
        if ((
    $diff > ($year)) && ($diff > ($month))) {
            
    $output round(($diff $year),0)." years ago";
        }
        elseif ((
    $diff $month) && ($diff > ($week))) {
            
    $output round(($diff $month),0)." months ago";
        }
        elseif ((
    $diff $week) && ($diff > (10 $day))) {
            
    $output round(($diff $week),0)." weeks ago";
        }
        elseif ((
    $diff $day) && ($diff > (36 $hour))) {
            
    $output round(($diff $day),0)." days ago";
        }
        elseif ((
    $diff $hour) && ($diff > (90 $min))) {
            
    $output round(($diff $hour),0)." hours ago";
        }
        elseif ((
    $diff $min) && ($diff > (90 $sec))) {
            
    $output round(($diff $min),0)." minutes ago";
        }
        else {
            
    $output $diff." seconds ago";
        }

        return 
    $output;


  5. #5
    SitePoint Zealot backtobasics's Avatar
    Join Date
    Aug 2006
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ace man, I'll give it a go, cheers!

  6. #6
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Umm Great function! mcmunt. The function works me fine but i am not sure about backtobasics.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  7. #7
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Do it all in SQL, rplace date1 with the last posting time

    Code:
     
    SELECT CONCAT(DAYOFYEAR(date1)-DAYOFYEAR(NOW()),' days ', DATE_FORMAT(ADDTIME("2000-00-00 00:00:00",SEC_TO_TIME(TIME_TO_SEC(date1)-TIME_TO_SEC(NOW()))),'%k hours and %i minutes')) AS time FROM time_table;
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  8. #8
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mcmunt View Post
    I use this function for showing something's age. Definitely not the smartest code but hey it works.
    PHP Code:
    function showSmartDate($date,$date2 NULL) {
        
    //...
        
    $month $day 30;
        
    $year $month 12;
        
    //...

    Are those interval definitions a little... vague?

  9. #9
    SitePoint Addict pkSML's Avatar
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    230
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might find this code useful --> http://code-bin.homedns.org/13

    I'm not sure how your data input is formatted, but you could use substr() to grab the values for $h1, $m1, $s1, $m1, $d1, and $y1.

    Example output: 7 days 3 hours 42 minutes 2 seconds

    If you just needed the value of days, http://code-bin.homedns.org/8 would work for ya'.
    -Stephen

    Get a LitlURL to this page!


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
  •