PHP Applications
Since writing my last post on the time_since() function, I became frustrated and tore apart the original script, to write my own time_since() function. This function is highly adaptable for those who want to have multiple phrases for the amount of time that has passed.
For example, if you would like to say "just a moment ago" for less than 10 seconds ago, or "about a minute ago" for time within a minute ago you can. You can even say your post was left "yesterday". I have not finished developing the script, but here is what i have done so far.
/*
------------------------------------------------------------
Time Ago Stamp
============================================================
*/
function time_since($mbtime){
/* Current unix time */
$now = time();
$diff = $now - $mbtime;
/* Convert PHP time/date to legiable data, to do the math */
$today = date('dmy', $now) === date('dmy', $mbtime) ? true : false;
/* Here is the fun part, how many phrases can you think of?
This may help: some Math.
the $diff is the amount of time that has passed. So to get the amount of
seconds, minutes, hours passed you need to divide by the figure you wish.
So say you want seconds:
$diff / 1
minutes:
$diff / 60
hours:
$diff / 60 * 60
days:
$diff / 60 * 60 * 24
The same equations can be made for defining if the time should be less
than an amount, so if you want to say something for less than 10 seconds
ago:
$diff < 10
less than an hour:
$diff < 60 * 60
*/
if($today && $diff < 10){ // if less than 10 seconds ago
$num = ceil($diff / 60);
return 'a moment ago';
}
elseif($today && $diff < 60){ // if less than a minute ago
$num = ceil($diff / 1);
return $num . ' second' . ($num > 1 ? 's' : '') . ' ago';
}
elseif($today && $diff < 60 * 1){ // if less than 1 minute ago
$num = ceil($diff / 60);
return 'about a minute ago';
}
elseif($today && $diff < 60 * 60){ // if less than an hour ago
$num = ceil($diff / 60);
return $num . ' minute' . ($num > 1 ? 's' : '') . ' ago';
}
elseif($today && $diff < 60 * 60 * 2){
$num = floor($diff / 60 / 60); // if hours ago
return 'a couple of hours ago';
}
elseif($today){
$num = floor($diff / 60 / 60); // if hours ago
return $num . ' hour' . ($num > 1 ? 's' : '') . ' ago';
}
else{
$thisyear = date('y', $now) === date('y', $mbtime) ? true : false;
$daydiff = date('z', $now) - date('z', $mbtime);
if($daydiff === 1 && $thisyear){
return 'Yesterday';
}
}
return $mbtime;
}
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";
break;
}
}
$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; }