Page 1 of 1

[solved] Problem with && in UDT

Posted: Thu May 13, 2010 9:35 am
by squarefrog
I'm having difficulty with one of my UDT's. I use the tag to compare 2 unix timestamps, and return XX minutes ago, or XX seconds ago. I got it working using simple > rules, but I'd quite like to include && so I can say "1 hour ago" rather than "1 hours ago".

Code: Select all

if($params['time']){

$postedTime =  $params['time'];
$timediff = time() - $postedTime;

	switch (TRUE)
	{
		case ($timediff > 60):
		return round($timediff / 60) . ' minutes ago';
 		break;
 	
		case ($timediff >= 3600 && < 7200):
		return '1 hour ago';
  		break;

		case ($timediff >= 7200):
 		return round($timediff / 3600) . ' hours ago';
  		break;
  	
		case ($timediff >= 86400 && < 172800):
		return '1 day ago';
  		break;
  		
  		case ($timediff >= 172800):
		return round($timediff / 86400) . ' days ago';
  		break;
  	
		default:
   		return $timediff . ' seconds ago';
	} 

}
Any suggestions?

Re: Problem with && in UDT

Posted: Thu May 13, 2010 9:54 am
by Jos
Change it to this:

Code: Select all

case ($timediff >= 3600 && $timediff < 7200):
edit:
and don't forget to add a max timediff here: case ($timediff >= 7200):

Re: Problem with && in UDT

Posted: Thu May 13, 2010 10:00 am
by squarefrog
Literally just figured it out as the reply came in. Good point about the max timediff. Needed one here too case ($timediff > 60):

Thanks for your help Jos!