note 89415 added to function.time
| From: | yasmary at gmail dot com | Date: | Fri, 06 Mar 2009 20:48:47 +0000 |
| Subject: | note 89415 added to function.time | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-151570@lists.php.net to get a copy of this message | ||
A time difference function that outputs the time passed in facebook's style: 1 day ago, or 4
months ago. I took andrew dot macrobert at gmail dot com function and tweaked it a bit. On a strict
enviroment it was throwing errors, plus I needed it to calculate the difference in time between a
past date and a future date.
<?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();
$unix_date = strtotime($date);
// check validity of date
if(empty($unix_date)) {
return "Bad date";
}
// is it future date or past date
if($now > $unix_date) {
$difference = $now - $unix_date;
$tense = "ago";
} else {
$difference = $unix_date - $now;
$tense = "from 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}";
}
$date = "2009-03-04 17:45";
$result = nicetime($date); // 2 days ago
?>
----
Server IP: 66.207.199.35
Probable Submitter: 99.255.198.227
----
Manual Page -- http://www.php.net/manual/en/function.time.php
Edit -- https://master.php.net/note/edit/89415
Del: integrated -- https://master.php.net/note/delete/89415/integrated
Del: useless -- https://master.php.net/note/delete/89415/useless
Del: bad code -- https://master.php.net/note/delete/89415/bad+code
Del: spam -- https://master.php.net/note/delete/89415/spam
Del: non-english -- https://master.php.net/note/delete/89415/non-english
Del: in docs -- https://master.php.net/note/delete/89415/in+docs
Del: other reasons-- https://master.php.net/note/delete/89415
Reject -- https://master.php.net/note/reject/89415
Search -- https://master.php.net/manage/user-notes.php