Time Ago Function In PHP

Hello, mates,

We got into various situations where we need to find the time elapsed from a DateTime or simply a timestamp . To find the solution for these kinds of situations we can use Time Ago Function in PHP. Our today’s tutorial is based on the implementation of time ago function in our program with a simple example.

Time Ago Function In PHP

Time Ago takes the timestamp as an input and subtract it with the current timestamp. Then it compares the remaining date with the predefined set of rules which determine the month, day, minute and even year. Then it calculates time difference by using the predefined set of rules which determine the second, minute, hour, day, month and year.

Let’s learn the functionality with a simple example

The following code will convert the timestamp to time ago like 1hour ago, 1day ago, 2days ago etc.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
function timeAgo($time_ago){
$cur_time   = time();
$time_elapsed   = $cur_time - $time_ago;
$seconds    = $time_elapsed ;
$minutes    = round($time_elapsed / 60 );
$hours      = round($time_elapsed / 3600);
$days       = round($time_elapsed / 86400 );
$weeks      = round($time_elapsed / 604800);
$months     = round($time_elapsed / 2600640 );
$years      = round($time_elapsed / 31207680 );
// Seconds
if($seconds <= 60){
    echo "$seconds seconds ago";
}
//Minutes
else if($minutes <=60){
    if($minutes==1){
        echo "one minute ago";
    }
    else{
        echo "$minutes minutes ago";
    }
}
//Hours
else if($hours <=24){
    if($hours==1){
        echo "an hour ago";
    }else{
        echo "$hours hours ago";
    }
}
//Days
else if($days <= 7){
    if($days==1){
        echo "yesterday";
    }else{
        echo "$days days ago";
    }
}
//Weeks
else if($weeks <= 4.3){
    if($weeks==1){
        echo "a week ago";
    }else{
        echo "$weeks weeks ago";
    }
}
//Months
else if($months <=12){
    if($months==1){
        echo "a month ago";
    }else{
        echo "$months months ago";
    }
}
//Years
else{
    if($years==1){
        echo "one year ago";
    }else{
        echo "$years years ago";
    }
}
}

?>

This is the PHP code which will convert the provided input into the elapsed time.

Now we have to call the function with the input

1
2
3
4
5
<?php
  $timeago="2017-09-09 09:09:09";
  $time_ago =strtotime($timeago);
  echo timeAgo($time_ago);
?>

This is how we can simply find elapsed time between a time and current timestamp in form of hour, minutes, second etc.

Hope this will help our fellow developers.

Our team is working hard to solve your real life development challenges. You can also email us your requirement to get tutorials you want.

You can also have a look at our popular tutorial on
UNDERSTANDING SQL COMPARISON OPERATORS
Preventing SQL Injection In PHP

So, guys, there are a lot more tutorials waiting for you so that you can learn PHP with ease

Because Hurdles aren’t really hurdles

2 Comments

  1. Nikhil Tomar said:

    Really Nice Post

    September 30, 2017
    Reply
    • Saurabh Dubey said:

      Thank you Nikhil

      September 30, 2017
      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *