PHP calculates how many years, how many months, and how many days are two dates apart

Source: Internet
Author: User
The first method, implemented using the PHP class library

 /** * Function: Calculates how many years, how many months, and how many days are two dates * param string $date 1[format: 2011-11-5] * param string $date 2[format such as: 2012-12-01] * return array array (' Year ', ' Month ', ' Day ');    */function diffdate ($date 1, $date 2) {$datetime 1 = new \datetime ($date 1);    $datetime 2 = new \datetime ($date 2);    $interval = $datetime 1->diff ($datetime 2);    $time [' y '] = $interval->format ('%Y ');    $time [' m '] = $interval->format ('%m ');    $time [' d '] = $interval->format ('%d ');    $time [' h '] = $interval->format ('%H ');    $time [' i '] = $interval->format ('%i ');    $time [' s '] = $interval->format ('%s ');    $time [' a '] = $interval->format ('%a '); Two time difference total days return $time;} # Use instance $sss = Diffdate (' 2015-12-25 12:30:30 ', ' 2015-12-26 15:00:00 ');p rint_r ($sss); # output array ([y] = [m] = 0 [d] = 1 [h] = [i] = [s] = [A] + 1)  

The second method, using the function implementation (also in the test ....) )

// 计算两个时间相差(时分秒)$aaa = strtotime('2015-12-26 16:33:33');$bbb = $aaa-time();echo gmstrftime('%Y %m %d %H:%M:%S',$aaa);exit;$left_time = 86400 - time()+strtotime($orderInfo['create_time']);echo:gmstrftime('%H:%M:%S',$left_time);

The above describes how PHP calculates two dates apart, how many months, how many days, including aspects of the content, want to be interested in the PHP tutorial friends helpful.

  • 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.