Daney11 Posted March 18, 2008 Share Posted March 18, 2008 Hey guys In my database i have a datetime field that shows, for example, "2008-03-18 20:00:00" that being the 18th March. And each user has one of the these datetime fields with Last Logged in. Im not sure on how to make it so It shows how long ago, like "Last Logged in: "2008-03-18 20:00:00" (2 hours ago)" Any ideas? Thanks Quote Link to comment Share on other sites More sharing options...
Cep Posted March 18, 2008 Share Posted March 18, 2008 You want the date time functions to show today's date and time then less the field value. Depending on how accurately you want to return the date time will depend on what you use in the date function. Quote Link to comment Share on other sites More sharing options...
Daney11 Posted March 18, 2008 Author Share Posted March 18, 2008 So im using $date = date("Y-m-d H:i:s"); // Converts The Date $lastloggedin = "2008-03-17 21:00:00"; How would i get the details from that? Thanks Quote Link to comment Share on other sites More sharing options...
Cep Posted March 18, 2008 Share Posted March 18, 2008 <?php $date = strtotime(date("Y-m-d H:i:s")); // Converts today now, to seconds $lastloggedin = strtotime("2008-03-17 21:00:00"); //converts then to seconds $diff = $date - $lastloggedin; ?> This will give you the difference in seconds which you can then use further code to produce a difference in hours, minutes, days etc. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 18, 2008 Share Posted March 18, 2008 There's also a datediff() function in MySQL. I haven't used it, but search for it in these forums and you should find an example. Ken Quote Link to comment Share on other sites More sharing options...
Daney11 Posted March 18, 2008 Author Share Posted March 18, 2008 Im upto $Week = '604800'; $Day = '86400'; $Hour = '3600'; $Minute = '60'; $Second = '1'; $date = strtotime(date("Y-m-d H:i:s")); $lastloggedin = strtotime("2008-03-07 08:00:00"); $datea = ($date - $lastloggedin); if ($datea >= $Week) { echo "1 Week Ago!"; } elseif ($datea >= $Day) { echo "1 Day Ago!"; } elseif ($datea >= $Hour) { echo "1 Hour Ago!"; } elseif ($datea >= $Minute) { echo "1 Minute Ago!"; } elseif ($datea >= $Second) { echo "1 Second Ago!"; } Is there any code out there that would genereate Last logged in: 1 Hour 2 minutes 34seconds ago etc? 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.