次のようなコード:
<?php
date_default_timezone_set("Europe/Tallinn");
$dates = Array(
'2012-11-30',
'2012-06-27',
'2012-03-04',
'2012-01-05',
'2012-01-04'
);
foreach ($dates as $d)
{
$unix_timestamp = strtotime($d);
//As no time set it shows time as 00:00:00, as a day beginning time
echo date("d.m.Y H:i:s", $unix_timestamp) . "<br>";
$current_unix_timestamp = time();
//Assuming all dates as in past
$diff = $current_unix_timestamp - $unix_timestamp;
echo ago($diff);
echo '<br><br>';
}
function ago($diff)
{
$second = 1;
$minute = $second * 60;
$hour = $minute * 60;
$day = $hour * 24;
$month = $day * 27.554551; //average, will not give you exact difference, but close enough
$year = $day * 365;
$ret = "";
if (floor($diff/$year)!=0)
{
$ret .= floor($diff/$year) . ' year' . (floor($diff/$year)>1?'(s)':'') . ' ';
$diff -= floor($diff/$year) * $year;
}
if (floor($diff/$month)!=0)
{
$ret .= floor($diff/$month) . ' month' . (floor($diff/$month)>1?'(s)':'') . ' ';
$diff -= floor($diff/$month) * $month;
}
if (floor($diff/$day)!=0)
{
$ret .= floor($diff/$day) . ' day' . (floor($diff/$day)>1?'(s)':'') . ' ';
$diff -= floor($diff/$day) * $day;
}
if (floor($diff/$hour)!=0)
{
$ret .= floor($diff/$hour) . ' hour' . (floor($diff/$hour)>1?'(s)':'') . ' ';
$diff -= floor($diff/$hour) * $hour;
}
if (floor($diff/$minute)!=0)
{
$ret .= floor($diff/$minute) . ' minute' . (floor($diff/$minute)>1?'(s)':'') . ' ';
$diff -= floor($diff/$minute) * $minute;
}
if (floor($diff/$second)!=0)
{
$ret .= floor($diff/$second) . ' seconds' . (floor($diff/$second)>1?'(s)':'') . ' ';
$diff -= floor($diff/$second) * $second;
}
$ret .= 'ago';
return $ret;
}
?>
2013-05-09 09:29 にこのような結果が得られます
30.11.2012 00:00:00
5 month(s) 22 day(s) 13 hour(s) 55 minute(s) 22 seconds(s) ago
27.06.2012 00:00:00
11 month(s) 13 day(s) 7 hour(s) 4 minute(s) 3 seconds(s) ago
04.03.2012 00:00:00
1 year 2 month(s) 11 day(s) 5 hour(s) 51 minute(s) 2 seconds(s) ago
05.01.2012 00:00:00
1 year 4 month(s) 15 day(s) 3 hour(s) 13 minute(s) 56 seconds(s) ago
04.01.2012 00:00:00
1 year 4 month(s) 16 day(s) 3 hour(s) 13 minute(s) 56 seconds(s) ago