note 89444 deleted from function.date by danbrown
| From: | danbrown@php.net | Date: | Fri, 13 Mar 2009 02:37:49 +0000 |
| Subject: | note 89444 deleted from function.date by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-151815@lists.php.net to get a copy of this message | ||
Note Submitter: davet15 at hotmail dot com
----
There is probably a much easier way to do this, but this function works.
This will return the approximate time elapsed since the unix_timestamp fed to it, in this format:
1 minute ago
x:xx (xx minutes ago)
x:xx (x hours ago)
x/x/x (x hours ago) [ if not the same date]
x/x/x (x days ago)
x/x/x (x years ago)
The function:
<?php
function getLastUpdate($date){
$time = time();
$d = $time - $date;
if($d < 60){
return "Less than 1 minute ago.";
}
if($d < 3600){
$result = floor($d/60);
$plural = ($result > 1) ? "s" : "";
return date('g:ia',$date)." ($result minute$plural ago)";
}
if($d < 86400){
$result = floor($d/(60*60));
$plural = ($result > 1) ? "s" : "";
$text = (date('n/j/y',$date) != date('n/j/y',$time)) ?
date('n/j/y',$date) : date('g:ia',$date);
return "$text ($result hour$plural ago)";
}
if($d < 2592000){
$result = floor($d/(60*60*24));
$plural = ($result > 1) ? "s" : "";
return date('n/j/y',$date)." ($result day$plural ago)";
}
$result = floor($d/(60*60*24*30));
$plural = ($result > 1) ? "s" : "";
return date('n/j/y',$date)." ($result year$plural ago)";
}
?>