note 95259 deleted from function.time by danbrown

From: Date: Tue, 22 Dec 2009 19:09:02 +0000
Subject: note 95259 deleted from function.time by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-164256@lists.php.net to get a copy of this message
Note Submitter: MentalPower at GMail dot com ---- Took the function that was posted by yasmary at gmail dot com and added the ability to pass in a DateTime object in addition to the string literal. Also added special handling of 0 seconds difference between the $date supplied and the current time(). <?php function nicetime($date) { if(empty($date)) { return "No date provided"; } $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade"); $lengths = array("60","60","24","7","4.35","12","10"); $now = time(); if ($date instanceof DateTime) { $unix_date = $date->getTimestamp(); } else { $unix_date = strtotime($date); } //Check validity of date if (empty($unix_date)) { return "Bad date"; } //Is it a date in the future or in the past if($now > $unix_date) { $difference = $now - $unix_date; $tense = "ago"; } elseif ($now < $unix_date) { $difference = $unix_date - $now; $tense = "from now"; } else { return "Now"; } for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) { $difference /= $lengths[$j]; } $difference = round($difference); if($difference != 1) { $periods[$j].= "s"; } return "$difference $periods[$j] $tense"; } ?>

« previous php.notes (#164256) next »