note 100785 added to datetime.diff

From: Date: Sat, 06 Nov 2010 04:57:05 +0000
Subject: note 100785 added to datetime.diff
Groups: php.notes 
Request: Send a blank email to php-notes+get-173808@lists.php.net to get a copy of this message
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'); ?> ---- Server IP: 69.147.83.197 Probable Submitter: 74.102.24.56 ---- Manual Page -- http://www.php.net/manual/en/datetime.diff.php Edit -- https://master.php.net/note/edit/100785 Del: integrated -- https://master.php.net/note/delete/100785/integrated Del: useless -- https://master.php.net/note/delete/100785/useless Del: bad code -- https://master.php.net/note/delete/100785/bad+code Del: spam -- https://master.php.net/note/delete/100785/spam Del: non-english -- https://master.php.net/note/delete/100785/non-english Del: in docs -- https://master.php.net/note/delete/100785/in+docs Del: other reasons-- https://master.php.net/note/delete/100785 Reject -- https://master.php.net/note/reject/100785 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#173808) next »