0

Good day guys, I am having a bit trouble of how can I make this like xx month/s ago and year/s ago if the days reach the number of months/years. Is it possible ? Anyway I'm making a messaging module. This is my code. Advance thank you for your help guys

<?php       
   date_default_timezone_set('Asia/Manila');
    $now = strtotime(date("Y-m-d H:i:s"));
    $date = strtotime($key->message_date); //this is the date where message was sent
    $dateDiff = abs($now - $date);
    $fullDays = floor($dateDiff/(60*60*24));
     if($fullDays==0)
     {
        echo " Today ";
     }
     else if($fullDays==1)
     {
        echo " Yesterday ";
     }
     else
     {
        echo $fullDays ." days ago";
     }
     $at=date('g:iA',$date)
    ?> at <?php echo $at?>
Jonas Dulay
  • 283
  • 4
  • 18
  • And what is working wrong? – u_mulder Feb 07 '16 at 11:53
  • I don't know how can I make it like 1 month ago for example... I cant just divide it by 30. What if the month has only 28 days? and it is already 1 month ago . How can I solve it? – Jonas Dulay Feb 07 '16 at 11:56
  • possible duplicate http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php – Wizard Feb 07 '16 at 12:22

1 Answers1

0

You can use this function you have Pass message time as an argument

function convert_time($ptime){
        $etime = time() - $ptime;

        if($etime < 1){
            return '0 seconds';
        }

        $a = array( 12 * 30 * 24 * 60 * 60  =>  'year',
                    30 * 24 * 60 * 60       =>  'month',
                    24 * 60 * 60            =>  'day',
                    60 * 60                 =>  'hour',
                    60                      =>  'minute',
                    1                       =>  'second'
                );

        foreach ($a as $secs => $str){
            $d = $etime / $secs;
            if($d >= 1){
                $r = round($d);
                return $r . ' ' . $str . ($r > 1 ? 's' : '') . ' ago';
            }
        }
    }

In this function you can get "(1) second/minute/hour/day/month/year ago"