PHP

Time Ago Script

In this tutorials I explains time ago script like facebook.

<?php
$provided_time ='1445324970';
function php_time_ago($provided_time){
  $time_difference = time() - $provided_time ;
  $seconds = $time_difference ;
  $minutes = round($time_difference / 60 );
  $hours = round($time_difference / 3600 );
  $days = round($time_difference / 86400 );
  $weeks = round($time_difference / 604800 );
  $months = round($time_difference / 2419200 );
  $years = round($time_difference / 29030400 );
  if($seconds <= 60){
     echo "$seconds seconds ago";
  }else if($minutes <=60){
     if($minutes==1){
        echo "one minute ago";
     }else{
        echo "$minutes minutes ago";
     }
  }else if($hours <=24){
     if($hours==1){
        echo "one hour ago";
     }else{
        echo "$hours hours ago";
     }
  }else if($days <= 7){
     if($days==1){
        echo "one day ago";
     }else{
        echo "$days days ago";
     }
  }else if($weeks <= 4){
     if($weeks==1){
        echo "one week ago";
     }
     else{
        echo "$weeks weeks ago";
     }
  }else if($months <=12){
      if($months==1){
        echo "one month ago";
      }else{
        echo "$months months ago";
      }
   }else{
      if($years==1){
         echo "one year ago";
      }else{
         echo "$years years ago";
      }
   }
}
echo php_time_ago($provided_time);
?>

Recommended Tutorials for you


Related posts

How to convert number to String in PHP

Anuj Kumar

How to Salt & Hash a Password using Sha256

Anuj Kumar

Array in PHP

Anuj Kumar

Strings in PHP

Anuj Kumar

PHP – Data Types

Anuj Kumar

Date And Time Formatting With PHP

Anuj Kumar

How to fetch data from mysql using PHP

Anuj Kumar

Password Hashing in PHP

Anuj Kumar

PHP Operators

Anuj Kumar

4 comments

Aisha Abdulrazak November 10, 2018 at 6:00 am

How did you get the $provided_time=’1445324970′;
In the time ago script??

Reply
Anuj kumar November 10, 2018 at 6:38 am

By using time function (time()) you will get this value

Reply
Aisha Abdulrazak October 1, 2018 at 7:49 am

How did you get the $provided_time=’1445324970′;
In the time ago script??

Reply
Anuj kumar October 2, 2018 at 3:33 am

By using time function (time()) you will get this value

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.