sw0o0sh Posted August 13, 2010 Share Posted August 13, 2010 function time_ago($timestamp) { //$months = array("Jan" => 31, "Feb" => 28, "Mar" => 31, "Apr" => 30, "May" => 31, "Jun" => 30, "Jul" => 31, "Aug" => 31, "Sept" => 30, "Oct" => 31, "Nov" => 30, "Dec" => 31); $curr_time = time(); $time_ago = $curr_time - $timestamp; if($time_ago < 60) // seconds { $ext = $time_ago . " seconds ago.."; } else { if($time_ago >= 60) // minutes { $time = floor($time_ago / 60); $seconds_remainder = $time_ago % 60; $ext = $time . " minutes " . $seconds_remainder . " seconds ago.."; if($time >= 60) // hours { $hours = floor($time / 60); $minutes = $time % 60; $ext = $hours . " hours " . $minutes . " minutes ago.."; if($hours >= 24) // days { $days = floor($hours / 24); $day_hours = $hours % 24; $ext = $days . " days " . $day_hours . " hours ago .."; } } } } return $ext; } It seems to work fine (was going to tackle Months but haven't done it yet), but anyway is there a better way to do this? I'm guessing there is as I wrote this up rather fast and I'm not that great with date math. Any suggestions are appreciated (besides maybe using ternaries which I'm working on atm). Link to comment https://forums.phpfreaks.com/topic/210605-converting-timestamp-to-a-xx-hours-xx-minutes-ago-format-my-take/ Share on other sites More sharing options...
Mr Nick Posted August 13, 2010 Share Posted August 13, 2010 You could try and code in the months so it displays them as well... that's why it's not working. PS: I coded a better version of this. Link to comment https://forums.phpfreaks.com/topic/210605-converting-timestamp-to-a-xx-hours-xx-minutes-ago-format-my-take/#findComment-1098709 Share on other sites More sharing options...
sw0o0sh Posted August 13, 2010 Author Share Posted August 13, 2010 It not working isn't the problem I'm just looking for suggestions on how to improve this Link to comment https://forums.phpfreaks.com/topic/210605-converting-timestamp-to-a-xx-hours-xx-minutes-ago-format-my-take/#findComment-1098710 Share on other sites More sharing options...
Mr Nick Posted August 13, 2010 Share Posted August 13, 2010 It not working isn't the problem I'm just looking for suggestions on how to improve this Honestly I put this code in and it didn't display months. Link to comment https://forums.phpfreaks.com/topic/210605-converting-timestamp-to-a-xx-hours-xx-minutes-ago-format-my-take/#findComment-1098711 Share on other sites More sharing options...
sw0o0sh Posted August 13, 2010 Author Share Posted August 13, 2010 It seems to work fine (was going to tackle Months but haven't done it yet), Link to comment https://forums.phpfreaks.com/topic/210605-converting-timestamp-to-a-xx-hours-xx-minutes-ago-format-my-take/#findComment-1098714 Share on other sites More sharing options...
Mr Nick Posted August 13, 2010 Share Posted August 13, 2010 It seems to work fine (was going to tackle Months but haven't done it yet), Then it doesn't work. Link to comment https://forums.phpfreaks.com/topic/210605-converting-timestamp-to-a-xx-hours-xx-minutes-ago-format-my-take/#findComment-1098716 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.