note 75004 added to function.time
| From: | joncampell at gmail dot com | Date: | Tue, 08 May 2007 08:07:15 +0000 |
| Subject: | note 75004 added to function.time | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-126317@lists.php.net to get a copy of this message | ||
Time difference both forward and backward, based on tristan's TimeAgo() function :)
function timeDiff($timestamp){
$now = time();
//If the difference is positive "ago" - negative "away"
($timestamp >= $now) ? $action = 'away' : $action = 'ago';
switch($action) {
case 'away':
$diff = $timestamp - $now;
break;
case 'ago':
default:
// Determine the difference, between the time now and the timestamp
$diff = $now - $timestamp;
break;
}
// Set the periods of time
$periods = array("second", "minute", "hour", "day",
"week", "month", "year", "decade");
// Set the number of seconds per period
$lengths = array(1, 60, 3600, 86400, 604800, 2630880, 31570560, 315705600);
// Go from decades backwards to seconds
for ($val = sizeof($lengths) - 1; ($val >= 0) && (($number = $diff /
$lengths[$val]) <= 1); $val--);
// Ensure the script has found a match
if ($val < 0) $val = 0;
// Determine the minor value, to recurse through
$new_time = $now - ($diff % $lengths[$val]);
// Set the current value to be floored
$number = floor($number);
// If required create a plural
if($number != 1) $periods[$val].= "s";
// Return text
$text = sprintf("%d %s ", $number, $periods[$val]);
return $text . $action;
}
If anyone knows of an easier way to do this, please comment.
----
Server IP: 69.39.81.135
Probable Submitter: 71.110.244.196
----
Manual Page -- http://www.php.net/manual/en/function.time.php
Edit -- https://master.php.net/note/edit/75004
Del: integrated -- https://master.php.net/note/delete/75004/integrated
Del: useless -- https://master.php.net/note/delete/75004/useless
Del: bad code -- https://master.php.net/note/delete/75004/bad+code
Del: spam -- https://master.php.net/note/delete/75004/spam
Del: non-english -- https://master.php.net/note/delete/75004/non-english
Del: in docs -- https://master.php.net/note/delete/75004/in+docs
Del: other reasons-- https://master.php.net/note/delete/75004
Reject -- https://master.php.net/note/reject/75004
Search -- https://master.php.net/manage/user-notes.php