Copy Code code as follows:
/**
* Friendly Date Time
*
* @param datetime $datetime Date Time
* @param int $size precise in place number
* @throws \invalidargumentexception
* @return String
*/
function Friendly_date ($datetime, $size =1)
{
if (Is_int ($datetime)) {
$datetime = new \datetime ($datetime);
}
if (!) ( $datetime instanceof \datetime)) {
throw new \invalidargumentexception (' invalid ' DateTime "object ');
}
$now = new \datetime ();
$interval = $now->diff ($datetime);
$intervalData = Array (
$interval->y, $interval->m, $interval->d,
$interval->h, $interval->i, $interval->s,
);
$intervalFormat = Array (' Year ', ' Month ', ' Day ', ' hour ', ' Sub ', ' Second ');
foreach ($intervalData as $index => $value) {
if ($value) {
$intervalData [$index] = $value. $intervalFormat [$index];
} else {
Unset ($intervalData [$index]);
Unset ($intervalFormat [$index]);
}
}
Return implode (', Array_slice ($intervalData, 0, $size));
}