note 85128 modified in function.time by aidan

From: Date: Wed, 15 Apr 2009 16:08:08 +0000
Subject: note 85128 modified in function.time by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-153336@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 distanceOfTimeInWords($from_time, time(), $include_seconds); } ?> --was-- 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); } ?> http://php.net/manual/en/function.time.php

« previous php.notes (#153336) next »