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 Link to comment https://forums.phpfreaks.com/topic/96676-datetime/ 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. Link to comment https://forums.phpfreaks.com/topic/96676-datetime/#findComment-494716 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 Link to comment https://forums.phpfreaks.com/topic/96676-datetime/#findComment-494719 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. Link to comment https://forums.phpfreaks.com/topic/96676-datetime/#findComment-494727 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 Link to comment https://forums.phpfreaks.com/topic/96676-datetime/#findComment-494731 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? Link to comment https://forums.phpfreaks.com/topic/96676-datetime/#findComment-494754 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.