Hi,
I am a newbie to PHP but currently in the process of building a support center. I have the basics up and running such as read ticket, reply to ticket, close ticket and search. Now, I am trying to get a last reply line in the ticket details area.
In the database I have a field called last_reply. I would like to be able to create a lastreply function that would calculate how long ago the reply was made.
Eg. year, month, day, hour, min, secs.
What I want from the functionality is that if it is only seconds then just display "22 sec(s)" or "3 hour(s) 4 min(s) 17 sec(s)"
Any light that anyone could share so I can create the function would be great.
Thank you,
John
PS. I am sorry if I have posted this in the wrong section of the forum. Let me know and will move it to the correct forum section. Thanks for your help.
Last Repy..
Moderator: General Moderators
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
from http://snippets.dzone.com/posts/show/3044
Code: Select all
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'),
);
$today = time(); /* Current unix time */
$since = $today - $original;
if($since > 604800) {
$print = date("M jS", $original);
if($since > 31536000) {
$print .= ", " . date("Y", $original);
}
return $print;
}
// $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 "<!-- It's $name -->\n";
break;
}
}
$print = ($count == 1) ? '1 '.$name : "$count {$name}s";
return $print . " ago";
}