thefollower Posted November 27, 2007 Share Posted November 27, 2007 Is there a way to print the time different depending on how long the time has been. So like if the user has not been logged in for 24 hours or more... it will say 1 day rather than 24 hours. And if its less than 1 hour it will say it in minutes... and if its greater than 1 hour but less than 1 day it will say minutes and hours? This is my query which i have done so far but don't know how to do the above checking! //get register date and now date $GetLoginDate = mysql_query("SELECT DATEDIFF(NOW(), LoginDate) AS LastLogin FROM userregistration WHERE UserID='{$_SESSION['Current_User']}'") or die(mysql_error()); $lastlogin = mysql_fetch_assoc($GetLoginDate); Quote Link to comment Share on other sites More sharing options...
Wes1890 Posted November 27, 2007 Share Posted November 27, 2007 Like this (keep in mind, 3600 is equal to an hour): if ( $post_time => time()-3600*24 ) { $show = "1 Day ago"; break; } or if ( $post_time => time()-(3600*24)*7 ) { $show = "1 Week ago"; break; } It's check if the post_time is equal or greater then the current time, minus 1 day (3600 seconds is 1 hour, so multiply by 24 makes 1 day) You can do this for each "show" you want (1 hour, 1 day, 2 days, 1 week, 1 month etc) Just put it in a switch statement or something Quote Link to comment Share on other sites More sharing options...
thefollower Posted November 27, 2007 Author Share Posted November 27, 2007 Yeh but that prints a fixed string meaning if a user had not logged in for like 60 days i'd have to make a huge case statement for all 60 days to do: print '1 day ago'; print '2 days ago'; etc etc i was hoping to echo the returned value of the query like Echo $lastlogin['LastLogin']';Echo'Days ago!'; Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted November 27, 2007 Share Posted November 27, 2007 Make a function that returns the time how you want. basically you get the time in seconds of last login so something like <?php function time_display($last_login){ $now = date("U"); $dif = $now-$last_login; if($diff < 3600){$return = "1 Hours";} elseif($dif <3600*2){$return = "2 Hours";} elseif($diff <3600*10){$return = "10 Hours";} elseif($diff < 3600*24){$return = "1 Day";} else{$return = "More than 1 Day";} return $return; } ?> Its not exact but will work Quote Link to comment Share on other sites More sharing options...
Wes1890 Posted November 27, 2007 Share Posted November 27, 2007 ^^ just think and do $ago_time = time()-$post_time; $day = 3600*24; $days_ago = ceil($ago_time / $day); echo "$days_ago days ago"; Ive not tested it, but it should work Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted November 27, 2007 Share Posted November 27, 2007 the idea is a more formmatted answer that doesn't give just X days Quote Link to comment 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.