note 85481 added to function.time
| From: | Anonymous at osu1 dot php dot net | Date: | Tue, 02 Sep 2008 00:30:21 +0000 |
| Subject: | note 85481 added to function.time | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-144063@lists.php.net to get a copy of this message | ||
A cleaner example (half the comparisons) of distanceOfTimeInWords() function below:
<?php
public static function distanceOfTimeInWords($fromTime, $toTime = 0, $showLessThanAMinute = false) {
$distanceInSeconds = round(abs($toTime - $fromTime));
$distanceInMinutes = round($distanceInSeconds / 60);
if ( $distanceInMinutes <= 1 ) {
if ( !$showLessThanAMinute ) {
return ($distanceInMinutes == 0) ? 'less than a minute' : '1
minute';
} else {
if ( $distanceInSeconds < 5 ) {
return 'less than 5 seconds';
}
if ( $distanceInSeconds < 10 ) {
return 'less than 10 seconds';
}
if ( $distanceInSeconds < 20 ) {
return 'less than 20 seconds';
}
if ( $distanceInSeconds < 40 ) {
return 'about half a minute';
}
if ( $distanceInSeconds < 60 ) {
return 'less than a minute';
}
return '1 minute';
}
}
if ( $distanceInMinutes < 45 ) {
return $distanceInMinutes . ' minutes';
}
if ( $distanceInMinutes < 90 ) {
return 'about 1 hour';
}
if ( $distanceInMinutes < 1440 ) {
return 'about ' . round(floatval($distanceInMinutes) / 60.0) . '
hours';
}
if ( $distanceInMinutes < 2880 ) {
return '1 day';
}
if ( $distanceInMinutes < 43200 ) {
return 'about ' . round(floatval($distanceInMinutes) / 1440) . '
days';
}
if ( $distanceInMinutes < 86400 ) {
return 'about 1 month';
}
if ( $distanceInMinutes < 525600 ) {
return round(floatval($distanceInMinutes) / 43200) . ' months';
}
if ( $distanceInMinutes < 1051199 ) {
return 'about 1 year';
}
return 'over ' . round(floatval($distanceInMinutes) / 525600) . '
years';
}
?>
----
Server IP: 117.104.160.194
Probable Submitter: 210.9.182.39
----
Manual Page -- http://www.php.net/manual/en/function.time.php
Edit -- https://master.php.net/note/edit/85481
Del: integrated -- https://master.php.net/note/delete/85481/integrated
Del: useless -- https://master.php.net/note/delete/85481/useless
Del: bad code -- https://master.php.net/note/delete/85481/bad+code
Del: spam -- https://master.php.net/note/delete/85481/spam
Del: non-english -- https://master.php.net/note/delete/85481/non-english
Del: in docs -- https://master.php.net/note/delete/85481/in+docs
Del: other reasons-- https://master.php.net/note/delete/85481
Reject -- https://master.php.net/note/reject/85481
Search -- https://master.php.net/manage/user-notes.php