Home >Backend Development >PHP Tutorial >Convert DateTime object to friendly time display

Convert DateTime object to friendly time display

WBOY
WBOYOriginal
2016-07-25 09:09:02992browse
Use DateInterval in PHP 5.x to convert DateTime objects into friendly time displays                                                                 
                                                                                                                                                                                                                                                                                                    



/**
* Friendly date and time
*
    * @param DateTime $datetime date and time
  1. * @param int $size accurate to digits
  2. * @throws InvalidArgumentException
  3. * @return string
  4. */
  5. function friendly_date($datetime, $size=1)
  6. {
  7.                                                                                                                                                     
  8.                                                                                                                                                                                            ‑‑    ‑‑‑‑‑‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ $interval = $now->diff($ datetime);
  9.                                                                                                                ,,,,,, $interval- >s,
  10. );
  11. $intervalFormat = array('year', 'month', 'day', 'hour', 'minute', 'second');
  12. foreach($intervalData as $index= & gt; $ value) {
  13. if ($ value) {
  14. $ intervaldata [$ Index] = $ value. $ IntervalFormat [$ Index]; dex]); n Unset ($ intervalFormat[$index]);
  15.                                                                                                                                                                     intervalFormat[$index]);
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