PHP converts the DateTime object to the implementation code of friendly time display. The code for copying the code is as follows: ** friendly date time ** @ paramDateTime $ datetime date time * @ paramint $ size precise to the number of digits * @ throwsInvalidArgumentException * @ returnstri
The 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 ));
}
The http://www.bkjia.com/PHPjc/324421.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/324421.htmlTechArticle 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 stri...