简体   繁体   中英

message date ago (xx month ago or years)

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?>

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM