I've recently been working on a new project that called for a fancy looking time and date system.
To solve this issue I've wrote one.
When used it outputs stuff such as:
30 seconds
2 mins
1 hour 13 mins
May 22, 2013
Plonk it in a PHP file and use it as dateify("timestamp");
It works out how long ago it is since that date.
You may want to add 'ago' after such as:
<?php echo dateify($post['stamp'])." ago."; ?>
Have fun, feel free to point out any issues you have with it.
function dateify($stamp = null) {
$current = time();
$differ = $current-$stamp;
if ($differ < 86400 && $differ > 30) {
// Below 24hrs.
$mins = round($differ/60);
if ($mins > 60) { $hours = round($mins/60); }
if ($hours > 1) { $word_h = "hours"; } else { $word_h = "hour"; }
if ($mins > 1) { $word_m = "mins"; } else { $word_h = "min"; }
if ($hours > 0) { $mins = $mins - ($hours * 60); }
if ($hours <= 0) {
$scentence = "{$mins} {$word_m}";
}
else {
$scentence = "{$hours} {$word_h} {$mins} {$word_m}";
}
return $scentence;
}
else if ($differ <= 30) {
// 30 Seconds ago.
return $differ." seconds";
}
else {
return substr(date('F',$stamp),0,3).chr(32).date('j, Y',$stamp);
}
}
I posted something very similar, but yours seems to be a bit more flexible: http://www.hawkee.com/snippet/9368/
I agree with the "over coding" statement but, this is certainly a good start. I urge looking further into the date() function (specifically the 'M' flag), as your last line could have been
date('M j, Y', $stamp);
Also your suggestion about appending 'ago' will provide undesired results after 24 hours. (ex: May 22, 2013 ago).
Sidenote: I've made a similar function some time ago here, if you'd like a friendly reference :) I hope to see more PHP contributions!