Time Since

Recently I have been transfixed on the time since functions offered by Natalie Downe (credit where credit is due). I discovered her code when developing a web application for my previous job at the Chafford Hundred Campus.

At the time I only altered some of the code to make it display how I wanted it too, (just one item, rather than two). But I did not spend too much time thinking about how to add extra comments about the time that has passed.

So with another job comes another project, and I wanted to use the same style of time since comments and tasks were left. Having seen other blogs and sites using similar functions where by they say "left a moment ago", I took another look at the script to see what I could do.

The solution is simple, for the developers out there, you have probably worked this out a long time ago, but for those starting out, here you go.

The whole script works out the time since your timestamp, so the solution to adding the comments about a moment ago is working out the math for the amount of seconds, minutes, hours, weeks etc you want you comment to be associated with. For example if you wanted to say your message was left a moment ago, if it was left 15 seconds or under you would add the following line in the array of "time period chunks":

array(15, ‘a moment ago’),

The possibiliies are endless. For a couple of weeks ago:

array(60 * 60 * 24 * 14, ‘a couple of weeks ago’),

I have left the code in its altered state below. If you do find it useful, please tell me so, and tell me where you have used it too.

 —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  — 
Time Ago Stamp
/* Works out the time since the entry post, takes a an argument in unix time (seconds) */
 function time_since($original) {
// array of time period chunks
$chunks = array( array(60 * 60 * 24 * 365 , ‘year’),
array(60 * 60 * 24 * 30 , ‘month’),
array(60 * 60 * 24 * 7 , ‘week’),
array(60 * 60 * 24 , ‘day’),
array(60 * 60 , ‘hour’),
array(60 , ‘minute’),
array(30 , ‘less than a minute’),  // Just add in math like this. array(1 , ‘second’), );
$today = time();
/* Current unix time */
$since = $today - $original;
// $j saves performing the count function each time around the loop
for ($i = 0, $j = count($chunks); $i < $j; $i++) {
 $seconds = $chunks[$i][0];
$name = $chunks[$i][1];
// finding the biggest chunk (if the chunk fits, break)
if (($count = floor($since / $seconds)) != 0) {
// DEBUG print "n";
$print = ($count == 1) ? ‘1 ‘.$name : "$count {$name}s";
if ($i + 1 < $j) {
// now getting the second item
$seconds2 = $chunks[$i + 1][0];
 $name2 = $chunks[$i + 1][1];
 return $print; }


Posted On: Thursday, April 17th, 2008 @ 5:23 pm by Ian Tearle


There are no comments on this entry. You should add one below.

Leave a comment

Be kind

If you would like to discuss anything about this post, please be polite and add your comments below.



No HTML is allowed. All links, and new lines will be automagically converted to HTML for you.
Notice to spammers, all links will be sanitized with rel="nofollow".

Contact Me

Get In Touch

If you would like to discuss anything about your next project please drop me a line.



Projects start at £99.00 per month
We normally book 2–3 months in advance.


No matter how small your project is, or if the prices above scare you, please do get in touch, I shall always listen and will advise freely alternative ideas or solutions. If you are a charity, let's talk!

Find Me On