Page 1 of 1

Last Repy..

Posted: Mon Sep 10, 2007 3:10 pm
by Johnlbuk
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.

Posted: Mon Sep 10, 2007 3:18 pm
by Kieran Huggins
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";

}

Posted: Tue Sep 11, 2007 12:23 pm
by Johnlbuk
What format would the last reply date and time need to be stored in the database as?

eg. timestamp, date/time, time

Thanks

Posted: Tue Sep 11, 2007 4:07 pm
by Johnlbuk
All sorted thanks.