PHP time ago calculation

The new web 2.0 displays time and date not in old style manner like 12.30 pm Jan 200 7. The new generation need relative time like 2 hours ago, 6 months ago, 4 years ago, very old etc. So if you save the timestamp of a particular content creation in database we can simply show this ‘ago time calculation’ using the following algorithm.

//$datefrom is the timestamp for the content , and you can leave the $dateto value to see the current delay

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
function TimeAgo($datefrom,$dateto=-1)
{
// Defaults and assume if 0 is passed in that
// its an error rather than the epoch
if($datefrom<=0) { return "A long time ago"; }
if($dateto==-1) { $dateto = time(); }
// Calculate the difference in seconds betweeen
// the two timestamps
$difference = $dateto - $datefrom;
// If difference is less than 60 seconds,
// seconds is a good interval of choice
if($difference < 60)
{
$interval = "s";
}
// If difference is between 60 seconds and
// 60 minutes, minutes is a good interval
elseif($difference >= 60 && $difference<60*60)
{
$interval = "n";
}
// If difference is between 1 hour and 24 hours
// hours is a good interval
elseif($difference >= 60*60 && $difference<60*60*24)
{
$interval = "h";
}
// If difference is between 1 day and 7 days
// days is a good interval
elseif($difference >= 60*60*24 && $difference<60*60*24*7)
{
$interval = "d";
}
// If difference is between 1 week and 30 days
// weeks is a good interval
elseif($difference >= 60*60*24*7 && $difference <
60*60*24*30)
{
$interval = "ww";
}
// If difference is between 30 days and 365 days
// months is a good interval, again, the same thing
// applies, if the 29th February happens to exist
// between your 2 dates, the function will return
// the 'incorrect' value for a day
elseif($difference >= 60*60*24*30 && $difference <
60*60*24*365)
{
$interval = "m";
}
// If difference is greater than or equal to 365
// days, return year. This will be incorrect if
// for example, you call the function on the 28th April
// 2008 passing in 29th April 2007. It will return
// 1 year ago when in actual fact (yawn!) not quite
// a year has gone by
elseif($difference >= 60*60*24*365)
{
$interval = "y";
}
// Based on the interval, determine the
// number of units between the two dates
// From this point on, you would be hard
// pushed telling the difference between
// this function and DateDiff. If the $datediff
// returned is 1, be sure to return the singular
// of the unit, e.g. 'day' rather 'days'
switch($interval)
{
case "m":
$months_difference = floor($difference / 60 / 60 / 24 /
29);
while (mktime(date("H", $datefrom), date("i", $datefrom),
date("s", $datefrom), date("n", $datefrom)+($months_difference),
date("j", $dateto), date("Y", $datefrom)) < $dateto)
{
$months_difference++;
}
$datediff = $months_difference;
// We need this in here because it is possible
// to have an 'm' interval and a months
// difference of 12 because we are using 29 days
// in a month
if($datediff==12)
{
$datediff--;
}
$res = ($datediff==1) ? "$datediff month ago" : "$datediff
months ago";
break;
case "y":
$datediff = floor($difference / 60 / 60 / 24 / 365);
$res = ($datediff==1) ? "$datediff year ago" : "$datediff
years ago";
break;
case "d":
$datediff = floor($difference / 60 / 60 / 24);
$res = ($datediff==1) ? "$datediff day ago" : "$datediff
days ago";
break;
case "ww":
$datediff = floor($difference / 60 / 60 / 24 / 7);
$res = ($datediff==1) ? "$datediff week ago" : "$datediff
weeks ago";
break;
case "h":
$datediff = floor($difference / 60 / 60);
$res = ($datediff==1) ? "$datediff hour ago" : "$datediff
hours ago";
break;
case "n":
$datediff = floor($difference / 60);
$res = ($datediff==1) ? "$datediff minute ago" :
"$datediff minutes ago";
break;
case "s":
$datediff = $difference;
$res = ($datediff==1) ? "$datediff second ago" :
"$datediff seconds ago";
break;
}
return $res;
}
22 Comments , , ,

22 Responses to “PHP time ago calculation”

  1. Binny V A February 2, 2008 at 12:00 am #

    When posting code, could you put it in pre tag? Otherwise the indentation is lost.

  2. Bill June 19, 2008 at 11:34 pm #

    Exactly what I was looking for! Great job. Thanks for sharing.

  3. mfields October 1, 2008 at 7:56 pm #

    Hi,
    Is there a license on this code…
    Looks great would love to use it!!!
    Thanks!!!

  4. Tom November 6, 2008 at 11:50 pm #

    Brilliant – just what I needed. Thank you.

  5. Venkatesan December 22, 2008 at 4:24 am #

    Really Great script. It helps me lot.
    Thanks.

  6. A1dezine April 2, 2009 at 9:02 am #

    Great stuff. I got what i was looking. Thank you very much to share this code here, appreciated .

  7. Travis May 31, 2009 at 11:04 pm #

    Very nice, works great! Thanks for sharing!

  8. Gopinathan June 30, 2009 at 12:46 pm #

    Hi,

    Great code. Thanks for sharing. Really good

  9. Matt July 5, 2009 at 11:55 pm #

    Fail, it’s littered with “&lt” and “&gt”.

  10. Tunde July 24, 2009 at 11:48 pm #

    minute ago not working. Others are working perfect. I use 2009-07-23 09:11:50 as starting and 2009-07-23 09:59:50 as ending date. Can anyone help me. If you get the solution send it to tfrancislinks@yahoo.com

  11. Theo January 28, 2010 at 12:06 pm #

    Great Work !

    Thanxs !!

  12. ben February 4, 2010 at 7:27 pm #

    Sorry mate, it didn’t work for me. Cheers.

  13. joomla March 3, 2010 at 11:38 am #

    add one line “$datefrom = strtotime($datefrom);” right after “function TimeAgo($datefrom,$dateto=-1)
    {” to display your time properly. I was thinking what is missing, but as you mention above mention line, it will work fine…

    thanks for the great code!

  14. Mike November 4, 2010 at 9:13 am #

    Great work, thanks for posting :)

  15. Murugavel February 3, 2011 at 1:56 pm #

    Hi buddy..

    Thanks for your post..

    Its great..

    Expecting more source codes from you man..

    :-o

  16. Jeremy July 28, 2011 at 1:35 am #

    This seems to work better

    http://pastebin.com/kAUKBmCc

  17. Allison October 7, 2011 at 6:04 pm #

    Thanks for the code, accurate and easy to plug in!

  18. Rykiel January 21, 2012 at 10:54 am #

    Very neat and well done!

  19. MARLON April 10, 2012 at 6:11 am #

    ARE WE IN PREP SCHOOL?

  20. fayis January 8, 2013 at 12:00 pm #

    ( ! ) Parse error: syntax error, unexpected ‘;’ in C:\wamp\www\newTest\forum.php on line 228

    228th line:

    if($datefrom<=0) { return “A long time ago”; }

Trackbacks/Pingbacks

  1. Fechas en formato tiempo transcurrido(time ago) con PHP - Bitácora de Webmaster - January 19, 2010

    [...] PHP Time Ago Calculation Soy Alfredo Artiles, Co-Fundador de equipo24 y e24Presenter. Mi pasión es el AJAX, SEO, SEM y [...]

Leave a Reply

More in Downloads, php, php source code (90 of 122 articles)


When you send mail from php code, you use mail() function for this purpose. Here is another library from ...