Copy codeThe Code is as follows:
/**
* Friendly Date and Time
*
* @ Param DateTime $ datetime Date and Time
* @ Param int $ size precise to the number of digits
* @ 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', 'hourly ', 'specificity', '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 ));
}