runnerjp Posted November 4, 2008 Share Posted November 4, 2008 i have created a function to get the time of a post and display the relavent text function getTime($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; } } but when i call it like so <? $dbtime=$getthreads3['lastrepliedto']; $time = date("F j, Y, g:i a", $dbtime); getTime($dbtime); ?> i get a blank page Quote Link to comment https://forums.phpfreaks.com/topic/131337-solved-function-gettime-not-working/ Share on other sites More sharing options...
JonnoTheDev Posted November 4, 2008 Share Posted November 4, 2008 Your case switch($intDiff) must be using the default condition: default: echo $time; The variable $time isn't defined anywhere in your function Quote Link to comment https://forums.phpfreaks.com/topic/131337-solved-function-gettime-not-working/#findComment-681987 Share on other sites More sharing options...
.josh Posted November 4, 2008 Share Posted November 4, 2008 Did you echo $dbtime and $intDiff to see if they are holding expected values? My first guess is that $dbtime is somehow not being set, causing your $intDiff to trigger your default (as neil pointed out). Quote Link to comment https://forums.phpfreaks.com/topic/131337-solved-function-gettime-not-working/#findComment-682009 Share on other sites More sharing options...
kenrbnsn Posted November 4, 2008 Share Posted November 4, 2008 Your switch statement is incorrect. You need to switch on the value "true" if you're going to use case statements like that. <?php switch(true) { 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; } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/131337-solved-function-gettime-not-working/#findComment-682017 Share on other sites More sharing options...
runnerjp Posted November 5, 2008 Author Share Posted November 5, 2008 ok here is where im at: im calling it like so.. <? $dbtime=$getthreads3['lastrepliedto']; $time = date("F j, Y, g:i a", $dbtime); getTime($dbtime,$time); ?> and my function is set up now as <?php /** * get time in forum * * @access public * @param none * @return string */ function getTime($dbtime,$time) { // Get current timestamp $intTime = time(); // Calculate difference $intDiff = $intTime - $dbtime; // Check time switch(true) { 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; }} ?> but im getting the error Missing argument 2 for getTime() Quote Link to comment https://forums.phpfreaks.com/topic/131337-solved-function-gettime-not-working/#findComment-682917 Share on other sites More sharing options...
runnerjp Posted November 5, 2008 Author Share Posted November 5, 2008 ok found it out... getTime must be a saved name or something! Quote Link to comment https://forums.phpfreaks.com/topic/131337-solved-function-gettime-not-working/#findComment-682942 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.