sw0o0sh Posted August 14, 2010 Share Posted August 14, 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; } Is there a cleaner approach to this? Link to comment https://forums.phpfreaks.com/topic/210690-converting-timestamp-to-a-xx-hours-xx-minutes-ago-format-my-take/ Share on other sites More sharing options...
PFMaBiSmAd Posted August 14, 2010 Share Posted August 14, 2010 Is there some reason you started a second thread for this same code instead of continuing in the same thread you started yesterday? Link to comment https://forums.phpfreaks.com/topic/210690-converting-timestamp-to-a-xx-hours-xx-minutes-ago-format-my-take/#findComment-1099096 Share on other sites More sharing options...
Daniel0 Posted August 14, 2010 Share Posted August 14, 2010 Quote Is there a cleaner approach to this? If you're using PHP 5.3: $timestamp = new DateTime('05:00'); $diff = $timestamp->diff(new DateTime()); echo $diff->format('%h hours, %i minutes'); See: http://php.net/datetime.diff http://php.net/dateinterval.format Link to comment https://forums.phpfreaks.com/topic/210690-converting-timestamp-to-a-xx-hours-xx-minutes-ago-format-my-take/#findComment-1099107 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.