PHP Time Ago

A simple way to display ago with UNIX time.



Displaying the time ago.
To display the time ago. Just add this where you want to display the time ago.

1
2
3
4
5
6
7
8
9
10
11
<?PHP
/*Created by Barrett at RRPowered.com*/
include_once('ago.php');//Include the ago.php file

$cur_time1 ="1291684422";//Your Unix time from database

echo time_ago($cur_time1) ;
?>

 

Here is the PHP file!
This file is called ago.php

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?PHP
/*Created by Barrett at RRPowered.com*/
//Call the function

function time_ago($cur_time){
$time_ = time() - $cur_time;

$seconds =$time_;
$minutes = round($time_ / 60);
$hours = round($time_ / 3600);
$days = round($time_ / 86400);
$weeks = round($time_ / 604800);
$months = round($time_ / 2419200);
$years = round($time_ / 29030400);

//Seconds

if($seconds <= 60){

   $time="$seconds seconds ago";   

//Minutes

}else if($minutes <= 60){

   if($minutes == 1){
   $time="one minute ago";
   }else{
   $time="$minutes minutes ago";
   }

//Hours

}else if($hours <= 24){

  if($hours == 1){
  $time="one hour ago";
  }else{
  $time="$hours hours ago";
  }

//Days

}else if($days <= 7){

   if($days == 1){
   $time="one day ago";
   }else{
   $time="$days days ago";
   }

//Weeks

}else if($weeks <= 4){

  if($weeks == 1){
  $time="one week ago";
  }else{
  $time="$weeks weeks ago";
  }

//Months

}else if($months <= 12){

  if($months == 1){
  $time="one month ago";
  }else{
  $time="$months months ago";
  }

//Years

}else{  

  if($year == 1){
  $time="one year ago";
  }else{
  $time="$year years ago";
  }  

}
return $time;
}
?>

ADVERTISEMENT

Tags: , , , ,

Enjoyed this Post?

Subscribe to our RSS Feed, Follow us on Twitter or simply recommend us to friends!

Discussion

  • Mizno Kruge

    wow… very useful function, Barret. thanks for sharing

    mizno kruge
    http://miznokruge.net

  • Kuttan

    thanks

  • http://www.itoctopus.com/ itoctopus

    That’s s a great function that can be used in the commenting system for any website (for example, comment was written 20 minutes ago)…

    Thanks for sharing!