runnerjp Posted October 30, 2008 Share Posted October 30, 2008 <? $dbtime = $getthreads3['lastrepliedto']; $dbtime=$getthreads3['showtime']; $time = date("F j, Y, g:i a", $dbtime); // Get current timestamp $intTime = time(); // Calculate difference $intDiff = $intTime - $dbtime; // Check time switch($intDiff) { case ($intDiff < 60): echo "<strong>Less than a minute ago</strong>"; break; case ($intDiff < 3600): echo "<strong>Less than an hour ago</strong>"; break; case ($intDiff < 7200): echo "<strong>One hour ago</strong>"; break; case ($intDiff < 10800): echo "<strong>Two hours ago</strong>"; break; case ($intDiff < 86400): echo "<strong>Today</strong>"; break; case ($intDiff < 172800): echo "<strong>Yesterday</strong>"; break; default: echo $time; } ?> the code works if its posted as a thread but for some reason if the thread is replied too it just shows default: echo $time;... any suggestions why? Link to comment https://forums.phpfreaks.com/topic/130733-displaying-when-message-was-posted/ Share on other sites More sharing options...
gaza165 Posted October 30, 2008 Share Posted October 30, 2008 What this means is that nothing was matched by the other cases so it goes to the default: case so therefor it will echo $time; Link to comment https://forums.phpfreaks.com/topic/130733-displaying-when-message-was-posted/#findComment-678429 Share on other sites More sharing options...
predator12341 Posted October 30, 2008 Share Posted October 30, 2008 really you should group them better as well for example case ($intDiff <= 7200 && $intDiff > 10800): echo "<strong>One hour ago</strong>"; this could cause some problems Link to comment https://forums.phpfreaks.com/topic/130733-displaying-when-message-was-posted/#findComment-678493 Share on other sites More sharing options...
Mchl Posted October 30, 2008 Share Posted October 30, 2008 I don't think switch can be used like that: switch($var) { case ($var < 1): break; case ($var < 2): break; default: break; } There's no such example in manual. I think this is a case (pun not intended) for if-else statement Link to comment https://forums.phpfreaks.com/topic/130733-displaying-when-message-was-posted/#findComment-678512 Share on other sites More sharing options...
predator12341 Posted October 30, 2008 Share Posted October 30, 2008 hmm yeah quite true Mchl. to be honest if i ever use a case it is for a straight statment anyway such as switch($name) { case"tim": break; case "bob": break; } i never do any sort of checking in a switch really. Link to comment https://forums.phpfreaks.com/topic/130733-displaying-when-message-was-posted/#findComment-678515 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.