How to add "5 mins ago" like in Facebook using PHP

Advertisemen

Here are the few examples of what are we going to build in this chapter.





In this example, we are going to assume that you want to get the post date saved in mysql database and display in the post.
Here is the piece of code used when you get date from database.
$postTime = $result["postTime"];



Now, create this function on the same page or save in different page and import it.

<?php
   
function ago($time)
    {
       
$now = time();
       
$secs = $now - strtotime($time);

       
$mins = round($secs / 60);
       
$hrs = round($secs / 3600); // 60*60
       
$days = round($secs / 86400); // 60*60*24
       
$weeks = round($secs / 604800); // 60*60*24*7
       
$months = round($secs / 2629440);
       
$years = round($secs / 31553280);

       
if($secs <= 60)

        {
           
$periods = "Just Now";
       
}
       
else if ($mins <= 60)
        {
            
if($mins == 1)
            {
               
$periods = "One minute ago";
           
}
           
else
           
{
               
$periods = $mins." mins ago";
           
}
        }
       
else if($hrs <= 24)
        {
           
if($hrs == 1)
            {
               
$periods = "an hour ago";
           
}
           
else
           
{
               
$periods = $hrs." hrs ago";
           
}
        }
       
else if($days <= 6)
        {
           
if($days == 1)
            {
                
$periods = "a day ago";
           
}
           
else
           
{
               
$periods = $days." days ago";
           
}
        }
       
else if($weeks <= 4.3)
        {
           
if($weeks == 1)
            {
               
$periods = "a week ago";
           
}
           
else
           
{
               
$periods = $weeks." weeks ago";
           
}
        }
       
else if($months <= 12)
        {
           
/*if($months == 1)
            {
                $periods = "a month ago";
            }
            else
            {
                $periods = $months." months ago";
            }*/
    $periods =date("F j" ,strtotime($result["postDate"]));
        }
       
else
       
{
           
/*if($years == 1)
            {
                $periods = "a year ago";
            }
            else
            {
                $periods = $months." years ago";
            }*/
   
 $periods =date("F j, Y" ,strtotime($result["postDate"]));
        }
       
return periods;
   
}
?>

Here is how we got the number for the months and years :
For months : ((365 + 365 365 365 + 366) / 5 / 12) * 24 * 60 * 60
For years : ((365 + 365 365 365 + 366) / 5 ) * 24 * 60 * 60

You might wondering what is 365 and 366. Nearly every 4 years is a leap year which has 366 days so we just calculated average years from these 5 years, rest is self explanatory.

Remember that whenever php throws time value, it is always in secs that's why there are many 60 to calculate minutes, hours, days and so on.

I have commented some of the codes above, just in case you want to display like, a month ago or a year ago instead of 1 March or 1 July, 1990.

time() function gets the current time and strtotime() converts string into time value. When you get the time from data base it in string format so we need to convert it. Learn more about these from php official website.

Other than that everything is pretty straight forward and self explanatory.

Ok now call this function wherever you want to display the time and pass the value you extracted from mysql database.
like this :

$postTime = $result["postTime"];
 echo ago($postTime);

If any of you guys have any problem understanding the code above, feel free to write in comment below.

Thanks


Advertisemen

Disclaimer: Gambar, artikel ataupun video yang ada di web ini terkadang berasal dari berbagai sumber media lain. Hak Cipta sepenuhnya dipegang oleh sumber tersebut. Jika ada masalah terkait hal ini, Anda dapat menghubungi kami disini.

Tidak ada komentar:

Posting Komentar

© Copyright 2017 Tutorial Unity 3D