This is arnorhs’ code, which converts a date & time into “human” timing, such as “3 days ago”:
$time = strtotime('2010-04-28 17:25:43');
echo 'event happened '.humanTiming($time).' ago';
function humanTiming ($time)
{
$time = time() - $time; // to get the time since that moment
$tokens = array (
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second');
foreach ($tokens as $unit => $text) {
if ($time < $unit) continue;
$numberOfUnits = floor($time / $unit);
return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
}
}
It works great when typing the date manually.
But when I use a string which contains a MySQL database DATETIME, the result is always “42 years”:
// MySQL date is: 2012-01-02 11:22:33
$date1 = mysql_result($result,0,"date");
$date2 = date("Y:m:d H:i", strtotime($date1));
echo humanTiming($date2);
// Result: "42 years"
Edit: Here’s my second attempt
$date1 = mysql_result($result,0,"date");
$date2 = "2012-01-02 11:22:33";
echo "The first date is $date1 which is ";
echo humanTiming( strtotime($date1));
echo ", and the second date is $date2 which is ";
echo humanTiming( strtotime($date2));
// Result: The first date is 2012-01-02 11:22:33 which is , and the second date is 2012-01-02 11:22:33 which is 6 months
That
humanTiming()function expects a UNIX timestamp, not a formatted date, so call it like this:With your sample date, the output is: