Reputation: 2066
here is my php code.....
<?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 "Incorrect Date";
}
// is it future date or past date
if($now > $unix_date) {
$difference = $now - $unix_date;
$tense = "ago";
} else {
$difference = $unix_date - $now;
$tense = "ago";
}
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}";
}
?>
but it is not showing the time correctly....and if any current time is given to it is showing 25-35 seconds ago and so on....but i want to make a time difference funtion like digg.com in a format like 2 Mints AND 3 Sec ago... how can i do that
Upvotes: 0
Views: 348
Reputation: 1814
Have a look at and learn from the Date::span
and Date::fuzzy_span
methods of the Kohana Date class.
Upvotes: 0
Reputation: 152206
Try it to do with the other way - at the beginning multiply all $lengths
values (i.e. $multiplied
).
1. Divide $difference
with that value.
2. If >= 1 then you have a solution.
Else divide $multiplied
with the array_pop($lengths)
value. Go to 2.
3. You have the solution.
I think it would be it.
BTW - $tense
is always "ago"
.
Upvotes: 1