Time Ago Function Helper Class in PHP

Today, We want to share with you Time Ago Function Helper Class in PHP.In this post we will show you convert Timestamp to Time Ago Function using PHP, hear for How to format time since xxx e.g. “4 minutes ago” similar to Stack Exchange sites we will give you demo and example for implement.In this post, we will learn about Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago… with an example.

Time Ago Function Helper Class in PHP

There are the Following The simple About How to convert timestamp to time ago in PHP ? Full Information With Example and source code.

As I will cover this Post with live Working example to develop PHP TimeAgo Func. (e.g. 8 hours ago) (Example), so the Time ago implementation in PHP is used for this example is following below.

PHP Time Ago Function program

This PHP Best Article is for converting given date into a time ago string like 2 hours age, 3 years ago or more.PHP TimeAgo functionality is used to display time in a different format.

Read Also:  Concatenation string and Class in VueJS

How to format time since xxx e.g. “4 minutes ago” similar to Stack Exchange sites.

Example Like

1 minute ago
1 hour ago
1 day ago
1 month ago
1 year ago

The simple PHP Based TimeAgo Function is a function that is commonly used to display time in human understandable format.PHP program to convert timestamp.Simple module, that displays the date in a “time ago” format.

functions.php

function timeAgo($last_date, $start_date = -1) {
    $res = "A long time ago";
    if ($last_date <= 0) {
        return $res;
    }
    if ($start_date == -1) {
        $start_date = time();
    }
    $total_diff_now = $start_date - $last_date;
    $timeago = '';
    if ($total_diff_now < 60) {
        $timeago = "s";
    } elseif ($total_diff_now >= 60 && $total_diff_now < 60 * 60) {
        $timeago = "n";
    } elseif ($total_diff_now >= 60 * 60 && $total_diff_now < 60 * 60 * 24) {
        $timeago = "h";
    } elseif ($total_diff_now >= 60 * 60 * 24 && $total_diff_now < 60 * 60 * 24 * 7) {
        $timeago = "d";
    } elseif ($total_diff_now >= 60 * 60 * 24 * 7 && $total_diff_now < 60 * 60 * 24 * 30) {
        $timeago = "ww";
    } elseif ($total_diff_now >= 60 * 60 * 24 * 30 && $total_diff_now < 60 * 60 * 24 * 365) {
        $timeago = "m";
    } elseif ($total_diff_now >= 60 * 60 * 24 * 365) {
        $timeago = "y";
    }
    switch ($timeago) {
        case "m":
            $months_total_diff_now = floor($total_diff_now / 60 / 60 / 24 / 29);
            while (mktime(date("H", $last_date), date("i", $last_date), date("s", $last_date), date("n", $last_date) + ($months_total_diff_now), date("j", $start_date), date("Y", $last_date)) < $start_date) {
                $months_total_diff_now++;
            }
            $timestempdiffernce = $months_total_diff_now;
            if ($timestempdiffernce == 12) {
                $timestempdiffernce--;
            }
            $res = ($timestempdiffernce == 1) ? "$timestempdiffernce month ago" : "$timestempdiffernce months ago";
            break;
        case "y":
            $timestempdiffernce = floor($total_diff_now / 60 / 60 / 24 / 365);
            $res = ($timestempdiffernce == 1) ? "$timestempdiffernce year ago" : "$timestempdiffernce years ago";
            break;
        case "d":
            $timestempdiffernce = floor($total_diff_now / 60 / 60 / 24);
            $res = ($timestempdiffernce == 1) ? "$timestempdiffernce day ago" : "$timestempdiffernce days ago";
            break;
        case "ww":
            $timestempdiffernce = floor($total_diff_now / 60 / 60 / 24 / 7);
            $res = ($timestempdiffernce == 1) ? "$timestempdiffernce week ago" : "$timestempdiffernce weeks ago";
            break;
        case "h":
            $timestempdiffernce = floor($total_diff_now / 60 / 60);
            $res = ($timestempdiffernce == 1) ? "$timestempdiffernce hour ago" : "$timestempdiffernce hours ago";
            break;
        case "n":
            $timestempdiffernce = floor($total_diff_now / 60);
            $res = ($timestempdiffernce == 1) ? "$timestempdiffernce minute ago" : "$timestempdiffernce minutes ago";
            break;
        case "s":
            $timestempdiffernce = $total_diff_now;
            $res = ($timestempdiffernce == 1) ? "$timestempdiffernce second ago" : "$timestempdiffernce seconds ago";
            break;
    }
    return $res;
}

Uses:

timeAgo(strtotime($row['updated_at']))

Web Programming Tutorials Example with Demo

Read :

Read Also:  How to change date format in PHP Codeigniter?

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about php time ago like facebook.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  PHP Time Ago Function Converting Timestamp
  2. Read Also:  Google Like Autosuggest Search Using PHP Ajax
  3. Read Also:  PHP Time Ago Helper Function
  4. Read Also:  How to calculate the difference between two dates using PHP?
  5. Read Also:  Laravel Get Absolute Path Document Root Base URL
  6. Read Also:  Global Config variable In Laravel 6 Example
  7. Read Also:  PHP Tutorial
  8. Read Also:  VueJS String substring() Method
  9. Read Also:  How to (Easily) Change Date Format in codeigniter?
  10. Read Also:  Custom jQuery Validate Validation Example | jquery validator.addmethod
CLOSEX