The following is a conversion of datetime date and time to adult \ ', \ 'months \', \ 'day \ ', \ 'hour \', \ 'sub-type \', \ 'Seconds \ 'to display. For more information, see.
The following example shows how to convert datetime to adult, month, day, hour, specificity, and second, for more information, see.
/**
* 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 ));
}