/ Published in: PHP
Simple function to recreate the distance_of_time_in_words function from rails.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** * @var timestamp * @var timestamp * @return string approximate time, for example: about 19 hours */ function getTimeDifferenceInWords($firstTime, $secondTime = '') { // convert to unix timestamps // if second time was not supplied, use current time // find out the difference in seconds $seconds = ($firstTime > $secondTime) ? $firstTime - $secondTime : $secondTime - $firstTime; if ($minutes == 0) { return 'less than a minute'; } if ($minutes == 1) { return 'a minute'; } if ($minutes < 45) { return $minutes . ' minutes'; } if ($hours <= 1) { return 'about 1 hour'; } if ($hours < 24) { return 'about ' . $hours . ' hours'; } if ($days == 1) { return 'about 1 day'; } if ($days < 30) { return 'about ' . $days . ' days'; } if ($months == 1) { return 'about 1 month'; } if ($months < 12) { return 'about ' . $months . ' months'; } if ($years == 1) { return 'about 1 year'; } return 'about ' . $years . ' years'; }