Reputation: 99
I have a function in which gets the date from a post and is supposed to write how long ago it was posted. When I use the function it says it was posted 43 years and 4 months ago for some odd reason.
function relativeTime($date,$precision=2)
{
$times=array(365*24*60*60 => "year",
30*24*60*60 => "month",
7*24*60*60 => "week",
24*60*60 => "day",
60*60 => "hour",
60 => "minute",
1 => "second");
$passed=time()-$date;
if($passed<5)
{
$output='less than 5 seconds ago';
}
else
{
$output=array();
$exit=0;
foreach($times as $period=>$name)
{
if($exit>=$precision || ($exit>0 && $period<60)) break;
$result = floor($passed/$period);
if($result>0)
{
$output[]=$result.' '.$name.($result==1?'':'s');
$passed-=$result*$period;
$exit++;
}
else if($exit>0) $exit++;
}
$output=implode(' and ',$output).' ago';
}
return $output;
}
I am entering the date using:
$date = date("Y-m-d H:i:s");
Is the $passed=time()-$date
off?
Thanks everyone!
Upvotes: 0
Views: 177
Reputation: 219924
There's a much better way to do this. Use DateTime and its associated classes. The code is simpler and more accurate because it takes time zones and daylight's savings time into account.
$datetime1 = new DateTime();
$datetime2 = new DateTime('2013-01-03 17:13:00');
$interval = $datetime1->diff($datetime2);
$elapsed = $interval->format('%y years, %m months, %a days, %h hours,
%i minutes, %S seconds');
$elapsed = str_replace(array('0 years,', ' 0 months,', ' 0 days,',
' 0 hours,', ' 0 minutes,'), '', $elapsed);
$elapsed = str_replace(array('1 years, ', ' 1 months, ', ' 1 days, ',
' 1 hours, ', ' 1 minutes'), array('1 year, ',
'1 month, ', ' 1 day, ', ' 1 hour, ', ' 1 minute'),
$elapsed);
echo $elapsed;
Upvotes: 1
Reputation: 31685
time()
gives you a timestamp, i.e. an integer that represents the number of seconds since 1970-01-01 00:00:00 UTC.
date("Y-m-d H:i:s")
gives you a string, e.g. 2013-05-13 02:04:34
When you subtract theses two values, the string is converted into an integer based on the initial characters of the string. This yields 2013, which does not affect the timestamp very much.
Use a common format for the input paramter and the value that you want to subtract it from. When doing calculations on time instances, timestamps are usually easier to handle than any string formats (which are for presentation, not calculation). I'd change the function definition to accept timestamps.
Upvotes: 2
Reputation: 41968
You are using $date = date("Y-m-d H:i:s");
as the input, which will return something akin to 2013-05-13 02:04:05
right now. The call to time()
will currently return a value near 1368403494
. Subtract the two from eachother, and the date string will be implicitly silently converted to an integer by PHP, so 2013
. That means it'll see your post as about 1368403494 seconds ago, not entirely accidentally ending up somewhere in January 1970, the start of the Unix timestamp age. So yep, that'll be some 43 years and a few months old.
Don't mix string and integer arithmetic, PHP's silent conversions make them hell.
Upvotes: 1