note 101029 added to datetime.diff

From: Date: Mon, 22 Nov 2010 07:49:13 +0000
Subject: note 101029 added to datetime.diff
Groups: php.notes 
Request: Send a blank email to php-notes+get-174240@lists.php.net to get a copy of this message
for PHP version 5.3 <?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; } ?> for PHP version 5.2 <?php public function ago($dt) { $dt = date_parse($dt); $now = date_parse(date("Y-m-d H:i:s")); $suffix = " ago"; if ($now['year'] != $dt['year']) return $this->pluralize($now['year'] - $dt['year'], "year") . $suffix; if ($now['month'] != $dt['month']) return $this->pluralize($now['month'] - $dt['month'], "month") . $suffix; if ($now['day'] != $dt['day']) return $this->pluralize($now['day'] - $dt['day'], "day") . $suffix; if ($now['hour'] != $dt['hour']) return $this->pluralize($now['hour'] - $dt['hour'], "hour") . $suffix; if ($now['minute'] != $dt['minute']) return $this->pluralize($now['minute'] - $dt['minute'], "minute") . $suffix; if ($now['second'] != $dt['second']) return $this->pluralize($now['second'] - $dt['second'], "second") . $suffix; return "just now"; } private function pluralize($count, $text) { return $count . (($count == 1) ? (" $text") : (" ${text}s")); } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 119.73.67.2 ---- Manual Page -- http://www.php.net/manual/en/datetime.diff.php Edit -- https://master.php.net/note/edit/101029 Del: integrated -- https://master.php.net/note/delete/101029/integrated Del: useless -- https://master.php.net/note/delete/101029/useless Del: bad code -- https://master.php.net/note/delete/101029/bad+code Del: spam -- https://master.php.net/note/delete/101029/spam Del: non-english -- https://master.php.net/note/delete/101029/non-english Del: in docs -- https://master.php.net/note/delete/101029/in+docs Del: other reasons-- https://master.php.net/note/delete/101029 Reject -- https://master.php.net/note/reject/101029 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#174240) next »