note 100785 deleted from datetime.diff by danbrown
| From: | danbrown@php.net | Date: | Sat, 06 Nov 2010 15:00:24 +0000 |
| Subject: | note 100785 deleted from datetime.diff by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-173811@lists.php.net to get a copy of this message | ||
Note Submitter: Sam Henry
----
A minor modification of acrion's function makes it possible to get the difference in time
between now and a past or future date by passing in a date/time string instead of a datetime object.
Change the 'away' and 'ago' strings to suit your needs.
<?php
function datetime_diff($datetime){
$now = new DateTime('now');
$interval = date_create($datetime)->diff($now);
$suffix = ($interval->invert) ? ' away' : ' 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;
}
//Example
echo datetime_diff('2000-01-01');
?>