Last Repy..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Johnlbuk
Forum Newbie
Posts: 19
Joined: Mon Sep 10, 2007 3:01 pm

Last Repy..

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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";

}
Johnlbuk
Forum Newbie
Posts: 19
Joined: Mon Sep 10, 2007 3:01 pm

Post 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
Johnlbuk
Forum Newbie
Posts: 19
Joined: Mon Sep 10, 2007 3:01 pm

Post by Johnlbuk »

All sorted thanks.
Post Reply