Home >Backend Development >PHP Tutorial >Convert datetime type date and time to Chinese representation_PHP tutorial

Convert datetime type date and time to Chinese representation_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:59:22982browse

The following is a method to convert datetime date and time into years', 'months', 'days', 'hours', 'minutes', and 'seconds' for display. Friends in need can refer to it.

The following is a method to convert datetime date and time into years', 'months', 'days', 'hours', 'minutes', and 'seconds' for display. Friends in need can refer to it.

/**
* Friendly date and time
*
* @param DateTime $datetime date time
* @param int $size accurate to 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', '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));
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631329.htmlTechArticleThe following is a method to convert datetime into adults', 'months', 'days', 'hours', 'Minutes' and 'Seconds' are displayed, friends in need can refer to it. Below is a datet...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn