note 85128 added to function.time

From: Date: Fri, 15 Aug 2008 20:46:59 +0000
Subject: note 85128 added to function.time
Groups: php.notes 
Request: Send a blank email to php-notes+get-143536@lists.php.net to get a copy of this message
So I was looking for an equivalent of Ruby on Rails time_ago_in_words function. I found the TimeAgo function in the comments here, but it doesn't quite behave like the Rails counterpart. In case you are looking or it as well, I've ported the Ruby code into PHP. Unfortunately, it doesn't look as clean as its Ruby counterpart, but it works the same: <?php /* * PHP port of Ruby on Rails famous distance_of_time_in_words method. * See http://api.rubyonrails.com/classes/ActionView/Helpers/DateHelper.html for more details. * * Reports the approximate distance in time between two timestamps. Set include_seconds * to true if you want more detailed approximations. * */ function distanceOfTimeInWords($from_time, $to_time = 0, $include_seconds = false) { $distance_in_minutes = round(abs($to_time - $from_time) / 60); $distance_in_seconds = round(abs($to_time - $from_time)); if ($distance_in_minutes >= 0 and $distance_in_minutes <= 1) { if (!$include_seconds) { return ($distance_in_minutes == 0) ? 'less than a minute' : '1 minute'; } else { if ($distance_in_seconds >= 0 and $distance_in_seconds <= 4) { return 'less than 5 seconds'; } elseif ($distance_in_seconds >= 5 and $distance_in_seconds <= 9) { return 'less than 10 seconds'; } elseif ($distance_in_seconds >= 10 and $distance_in_seconds <= 19) { return 'less than 20 seconds'; } elseif ($distance_in_seconds >= 20 and $distance_in_seconds <= 39) { return 'half a minute'; } elseif ($distance_in_seconds >= 40 and $distance_in_seconds <= 59) { return 'less than a minute'; } else { return '1 minute'; } } } elseif ($distance_in_minutes >= 2 and $distance_in_minutes <= 44) { return $distance_in_minutes . ' minutes'; } elseif ($distance_in_minutes >= 45 and $distance_in_minutes <= 89) { return 'about 1 hour'; } elseif ($distance_in_minutes >= 90 and $distance_in_minutes <= 1439) { return 'about ' . round(floatval($distance_in_minutes) / 60.0) . ' hours'; } elseif ($distance_in_minutes >= 1440 and $distance_in_minutes <= 2879) { return '1 day'; } elseif ($distance_in_minutes >= 2880 and $distance_in_minutes <= 43199) { return 'about ' . round(floatval($distance_in_minutes) / 1440) . ' days'; } elseif ($distance_in_minutes >= 43200 and $distance_in_minutes <= 86399) { return 'about 1 month'; } elseif ($distance_in_minutes >= 86400 and $distance_in_minutes <= 525599) { return round(floatval($distance_in_minutes) / 43200) . ' months'; } elseif ($distance_in_minutes >= 525600 and $distance_in_minutes <= 1051199) { return 'about 1 year'; } else { return 'over ' . round(floatval($distance_in_minutes) / 525600) . ' years'; } ?> Additionally, here is the wrapper function: <?php /* * Like distanceOfTimeInWords, but where to_time is fixed to the output of time() * */ function timeAgoInWords($from_time, $include_seconds = false) { return $this->distanceOfTimeInWords($from_time, time(), $include_seconds); } ?> ---- Server IP: 208.69.120.35 Probable Submitter: 216.207.245.1 ---- Manual Page -- http://www.php.net/manual/en/function.time.php Edit -- https://master.php.net/note/edit/85128 Del: integrated -- https://master.php.net/note/delete/85128/integrated Del: useless -- https://master.php.net/note/delete/85128/useless Del: bad code -- https://master.php.net/note/delete/85128/bad+code Del: spam -- https://master.php.net/note/delete/85128/spam Del: non-english -- https://master.php.net/note/delete/85128/non-english Del: in docs -- https://master.php.net/note/delete/85128/in+docs Del: other reasons-- https://master.php.net/note/delete/85128 Reject -- https://master.php.net/note/reject/85128 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#143536) next »