note 97880 added to datetime.diff
| From: | acrion at gmail dot com | Date: | Thu, 13 May 2010 15:52:50 +0000 |
| Subject: | note 97880 added to datetime.diff | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-169219@lists.php.net to get a copy of this message | ||
If you want to quickly scan through the resulting intervals, you can use the undocumented properties
of DateInterval.
The function below returns a single number of years, months, days, hours, minutes or seconds between
the current date and the provided date. If the date occurs in the past (is negative/inverted), it
suffixes it with 'ago'.
<?php
function pluralize( $count, $text )
{
return $count . ( ( $count == 1 ) ? ( " $text" ) : ( " ${text}s" ) );
}
function ago( $datetime )
{
$interval = date_create('now')->diff( $datetime );
$suffix = ( $interval->invert ? ' ago' : '' );
if ( $v = $interval->y >= 1 ) return pluralize( $interval->y, 'year' ) .
$suffix;
if ( $v = $interval->m >= 1 ) return pluralize( $interval->m, 'month' ) .
$suffix;
if ( $v = $interval->d >= 1 ) return pluralize( $interval->d, 'day' ) . $suffix;
if ( $v = $interval->h >= 1 ) return pluralize( $interval->h, 'hour' ) .
$suffix;
if ( $v = $interval->i >= 1 ) return pluralize( $interval->i, 'minute' ) .
$suffix;
return pluralize( $interval->s, 'second' ) . $suffix;
}
?>
----
Server IP: 64.71.164.2
Probable Submitter: 24.103.189.84
----
Manual Page -- http://www.php.net/manual/en/datetime.diff.php
Edit -- https://master.php.net/note/edit/97880
Del: integrated -- https://master.php.net/note/delete/97880/integrated
Del: useless -- https://master.php.net/note/delete/97880/useless
Del: bad code -- https://master.php.net/note/delete/97880/bad+code
Del: spam -- https://master.php.net/note/delete/97880/spam
Del: non-english -- https://master.php.net/note/delete/97880/non-english
Del: in docs -- https://master.php.net/note/delete/97880/in+docs
Del: other reasons-- https://master.php.net/note/delete/97880
Reject -- https://master.php.net/note/reject/97880
Search -- https://master.php.net/manage/user-notes.php