Change time and date to "2 month 3 days"

Discussion in 'PHP' started by ridesign, Apr 9, 2009.

  1. #1
    Hi,
    I currently have this function and it gives me "2 months ago" if the date is = Febuary 6th 2009, but I also want it to add the words "2 months ago 3 days"

    Also is this the best way to do it, or is it resource intensive?


    if(!function_exists('how_long_ago')){
            function how_long_ago($timestamp){
                $difference = time() - $timestamp;
    
                if($difference >= 60*60*24*365){        // if more than a year ago
                    $int = intval($difference / (60*60*24*365));
                    $s = ($int > 1) ? 's' : '';
                    $r = $int . ' year' . $s . ' ago';
                } elseif($difference >= 60*60*24*7*5){  // if more than five weeks ago
                    $int = intval($difference / (60*60*24*30));
                    $s = ($int > 1) ? 's' : '';
                    $r = $int . ' month' . $s . ' ago';
                } elseif($difference >= 60*60*24*7){        // if more than a week ago
                    $int = intval($difference / (60*60*24*7));
                    $s = ($int > 1) ? 's' : '';
                    $r = $int . ' week' . $s . ' ago';
                } elseif($difference >= 60*60*24){      // if more than a day ago
                    $int = intval($difference / (60*60*24));
                    $s = ($int > 1) ? 's' : '';
                    $r = $int . ' day' . $s . ' ago';
                } elseif($difference >= 60*60){         // if more than an hour ago
                    $int = intval($difference / (60*60));
                    $s = ($int > 1) ? 's' : '';
                    $r = $int . ' hour' . $s . ' ago';
                } elseif($difference >= 60){            // if more than a minute ago
                    $int = intval($difference / (60));
                    $s = ($int > 1) ? 's' : '';
                    $r = $int . ' minute' . $s . ' ago';
                } else {                                // if less than a minute ago
                    $r = 'moments ago';
                }
    
                return $r;
            }
        }
    PHP:
     
    ridesign, Apr 9, 2009 IP
  2. ridesign

    ridesign Peon

    Messages:
    294
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    is this possible
     
    ridesign, Apr 11, 2009 IP
  3. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #3
    "Is this possible?" Frightens me about the future of the human race that someone would even phrase such a question. Think about it. Assuming you're willing to pretend a month is 30 days (which you seem to be), then:

    $months = floor($difference / 30);
    $days = $difference - ($months * 30);
    Code (markup):
     
    SmallPotatoes, Apr 12, 2009 IP