Project Details

Time Ago System in PHP

PHP Project

This PHP tutorial is for converting given date into a time ago string like 2 hours age, 3 years ago. In a previous tutorial, we have seen date format conversion by using PHP inbuilt functions. In this example, we are having a custom function to change a date format into time ago string.

The date/time functions allow you to get the date and time from the server where your PHP script runs. You can then use the date/time functions to format the date and time in several ways.

Note: These functions depend on the locale settings of your server. Remember to take daylight saving time and leap years into consideration when working with these functions.

The PHP date/time functions are part of the PHP core. No installation is required to use these functions.

The PHP date() function formats a timestamp to a more readable date and time.

Get a Date in PHP

The required format parameter of the date() function specifies how to format the date (or time).

Here are some characters that are commonly used for dates:

  • d - Represents the day of the month (01 to 31)
  • m - Represents a month (01 to 12)
  • Y - Represents a year (in four digits)
  • l (lowercase 'L') - Represents the day of the week

Other characters, like"/", ".", or "-" can also be inserted between the characters to add additional formatting.

The example below formats today's date in three different ways:

<?php
echo "Today is " . date("Y/m/d") . "<br>";
echo "Today is " . date("Y.m.d") . "<br>";
echo "Today is " . date("Y-m-d") . "<br>";
echo "Today is " . date("l");
?>

It will show dates like this :

Today is 2020/02/05
Today is 2020.02.05
Today is 2020-02-05
Today is Wednesday

But if you want to show this in Time Ago system then we have to add this date into a function which will convert it into time ago system, below is an example of time ago system in php.

<?php

function time_ago_in_php($timestamp){
  
  date_default_timezone_set("Asia/Karachi");         
  $time_ago        = strtotime($timestamp);
  $current_time    = time();
  $time_difference = $current_time - $time_ago;
  $seconds         = $time_difference;
  
  $minutes = round($seconds / 60); // value 60 is seconds  
  $hours   = round($seconds / 3600); //value 3600 is 60 minutes * 60 sec  
  $days    = round($seconds / 86400); //86400 = 24 * 60 * 60;  
  $weeks   = round($seconds / 604800); // 7*24*60*60;  
  $months  = round($seconds / 2629440); //((365+365+365+365+366)/5/12)*24*60*60  
  $years   = round($seconds / 31553280); //(365+365+365+365+366)/5 * 24 * 60 * 60
                
  if ($seconds <= 60){

    echo "Just Now";

  } else if ($minutes <= 60){

    if ($minutes == 1){

      echo "one minute ago";

    } else {

      echo "$minutes minutes ago";

    }

  } else if ($hours <= 24){

    if ($hours == 1){

      echo "an hour ago";

    } else {

      echo "$hours hrs ago";

    }

  } else if ($days <= 7){

    if ($days == 1){

      echo "yesterday";

    } else {

      echo "$days days ago";

    }

  } else if ($weeks <= 4.3){

    if ($weeks == 1){

      echo "a week ago";

    } else {

      echo "$weeks weeks ago";

    }

  } else if ($months <= 12){

    if ($months == 1){

      echo "a month ago";

    } else {

      echo "$months months ago";

    }

  } else {
    
    if ($years == 1){

      echo "one year ago";

    } else {

      echo "$years years ago";

    }
  }
}

time_ago_in_php("2018-06-02 15:5:00");
?>

You can easily download this script by clicking on the download button bellow.

Login to Download

Our Services

Below is the list of other services that we are providing to our valuable customers.

If you want to Grab any of our Service you can contact us on given contacts

  • Website Development
  • Software Development
  • Android Apps Development
  • Hosting + Domain
  • Search Engine Marketing (SEM)
  • Search Engine Optimization (SEO)
  • Social Media Marketing (SMM)
  • SMS Marketing
  • Email Marketing
  • Wordpress Development
  • PHP Open Cart Development
  • Digital Media Marketing
  • Digital Brand Promotion
  • Graphic Designing
  • Video Animation

Search Projects

Enter some keywords to search

Other Projects / Scripts

Snake Game in C / C++ Free Dow.....


C++ Project

Snake Game project is a desktop application which is developed in C/C++ platform. This C/C++ project with tutorial and guide for developing a code. Sn.....

View Details

Online Book Store Project in P.....


PHP / MYSQL Project

Online Book Store Project in PHP and MySQL, Online Book Store Project is a Web-Based Online e-book Shopping Project. It is the graphical user interfac.....

View Details

Ecommerce website in PHP / MyS.....


Open Cart Project

Free download Ecommerce Website in php / mysql framework Opencart, Free download ecommerce website in php, Ecommerce website in php free download , On.....

View Details

WHMCS Free Download - Hosting.....


PHP / MYSQL Project

WHMCS is an all-in-one client management, billing and support solution for online hosting businesses. WHMCS handles everything from signup to termina.....

View Details

University Management System -.....


PHP / MYSQL Project

University management system in PHP and MSQL that make a bridge between admin , teachers and parents.

View Details