PHP implementing code to convert a DateTime object to a friendly time display _php tutorial

Source: Internet
Author: User
Copy CodeThe code is as follows:
/**
* Friendly Date and time
*
* Date Time @param datetime $datetime
* @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 ', ' minute ', ' 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));
}

http://www.bkjia.com/PHPjc/324421.html www.bkjia.com true http://www.bkjia.com/PHPjc/324421.html techarticle Copy the code code as follows:/** * Friendly Date Time * * @param DateTime $datetime Date time * @param int $size exact number of in place * @throws \invalidargumentexceptio n * @return stri ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.