note 95259 added to function.time
| From: | MentalPower at GMail dot com | Date: | Tue, 22 Dec 2009 03:26:41 +0000 |
| Subject: | note 95259 added to function.time | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-164204@lists.php.net to get a copy of this message | ||
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";
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 24.138.234.110
----
Manual Page -- http://www.php.net/manual/en/function.time.php
Edit -- https://master.php.net/note/edit/95259
Del: integrated -- https://master.php.net/note/delete/95259/integrated
Del: useless -- https://master.php.net/note/delete/95259/useless
Del: bad code -- https://master.php.net/note/delete/95259/bad+code
Del: spam -- https://master.php.net/note/delete/95259/spam
Del: non-english -- https://master.php.net/note/delete/95259/non-english
Del: in docs -- https://master.php.net/note/delete/95259/in+docs
Del: other reasons-- https://master.php.net/note/delete/95259
Reject -- https://master.php.net/note/reject/95259
Search -- https://master.php.net/manage/user-notes.php