Friday, April 11, 2014

How to Show Timestamp like Seconds and Minutes Ago

Here i am talking about the timestamp which looks like Twitter or Facebook timestamp , 2 Days ago or 4 Days ago or 2 Minutes ago. Same like here is the simple tutorial which will show you how to implement same like this. Once you need to call this function timeAgo($timestamp);and you will just see this stamp one more thing change your timezone according to your taste or server as you wish. Also for the simple way just make your date like "2014-04-11 11:10:20" and take this in $timestamp variable and just call your function you will see these type of timestamp.

function timeAgo($timestamp){
date_default_timezone_set("Asia/Karachi");
    $datetime1=new DateTime("now");
    $datetime2=date_create($timestamp);
    $diff=date_diff($datetime1, $datetime2);
    $timemsg='';
    if($diff->y > 0){
        $timemsg = $diff->y .' year'. ($diff->y > 1?"'s":'');

    }
    else if($diff->m > 0){
     $timemsg = $diff->m . ' month'. ($diff->m > 1?"'s":'');
    }
    else if($diff->d > 0){
     $timemsg = $diff->d .' day'. ($diff->d > 1?"'s":'');
    }
    else if($diff->h > 0){
     $timemsg = $diff->h .' hour'.($diff->h > 1 ? "'s":'');
    }
    else if($diff->i > 0){
     $timemsg = $diff->i .' minute'. ($diff->i > 1?"'s":'');
    }
    else if($diff->s > 0){
     $timemsg = $diff->s .' second'. ($diff->s > 1?"'s":'');
    }

$timemsg = $timemsg.' ago';
echo $timemsg;
}

If you love this tutorials please share it or comment below.

0 comments:

Post a Comment

Please Don't write wrong comments.
.
Please write good comments or mistakes ...