I'm lookin for a function to format time difference.
Like format(timestamp) would return how long timestamp is, like format(10) would return 10 seconds... format(120) returns 2 minutes.
I would code it myself if it weren't for the complications of different days in months, leap years, etc.. because if the time i give it is 2 years and a day i want it to return 2 Years, 1 Day
I would be using it on a forum system to show how old each post is
(posted 5 minutes ago/posted 3 months, 2 weeks, 4 days ago)
Something like that
is there any classes or functions out there to do this?
I would also want it to round off, like if the time is over a week dont bother displaying the minutes and seconds (x weeks, x days, x hours), if its over a month then return (x months, x weeks, x days)
Time difference
Moderator: General Moderators
Actually I always check the manual before posting, the only function that looks like it could work is strptime
so thats why i posted here is to see if there is a user defined function equivalent to that.
(my test server is on windows, forgot to mention that lil detail)Note: This function is not implemented on Windows platforms.
so thats why i posted here is to see if there is a user defined function equivalent to that.
-
Robert Plank
- Forum Contributor
- Posts: 110
- Joined: Sun Dec 26, 2004 9:04 pm
- Contact:
I have a function that I wrote for a script of mine that does this exactly, you can use it as long as my name is left in the comments.
You can either put in two timestamps or just do dateToText(120) and get 2 minutes. Ignores 0 values, like if there are 0 hours. Oh yeah and you can edit the units like take out weeks if you want to by editing that array.
Code: Select all
<?php
/**
Compares two Unix timestamps and outputs the result in a human-readable form.
by Robert Plank -- http://www.jumpx.com
@param $time1 the first timestamp
@param $time2 the second timestamp
@param $precision how detailed the comparison should be
a precision of 2 might yield: "1 year, 1 month" or "2 weeks, 4 days"
a precision of 4 might yield: "1 year, 1 month, 6 days, 5 hours"
a precision of 0 will be as detailed as possible
@param separator the token to separate units with (space, comma), etc.
comment by default
Examples:
echo dateToText(strtotime("now"), strtotime("December 18th, 2004 02:56 PM PDT"), 0);
echo dateToText(strtotime("now"), strtotime("December 18th, 2004 02:56 PM PDT"), 2, " ");
echo dateToText(120);
*/
function dateToText($time1, $time2=0, $precision=2, $separator=', ') {
$difference = abs($time1 - $time2);
// Named unit and the number of seconds it takes up
$units = array(
"second" => 1,
"minute" => 60,
"hour" => 3600,
"day" => 86400,
"week" => 604800,
"month" => 2592000,
"year" => 31536000
);
$output = "";
$i = 0;
while (true) {
reset($units);
if ($difference >= end($units)) {
end($units);
}
else {
reset($units);
while ($difference >= next($units)) ;
prev($units);
}
list($unit, $value) = each($units);
$magnitude = floor($difference / $value);
$saveUnit = ($magnitude == 1) ? $unit : $unit."s";
$output[] = array($magnitude, $saveUnit);
$difference = $difference % $value;
if ($precision > 0 && count($output) >= $precision || $difference == 0) {
break;
}
}
foreach ($output as $key => $value) {
if ($value > 0) $output[$key] = implode(' ', $value);
}
return implode($separator, $output);
}
?>-
Robert Plank
- Forum Contributor
- Posts: 110
- Joined: Sun Dec 26, 2004 9:04 pm
- Contact: