Php calculates the number of years, months, and days between two dates.

Source: Internet
Author: User
: This article describes how many years, months, and days are separated by two dates in php. For more information about PHP tutorials, see. The first method is implemented using the PHP class library.

/*** Function: calculate the number of years, months, and days between two dates. * param string $ date1 [Format: 2011-11-5] * param string $ date2 [format: 2012-12-01] * return array ('year', 'month', 'day'); */function diffDate ($ date1, $ date2) {$ datetime1 = new \ DateTime ($ date1); $ datetime2 = new \ DateTime ($ date2); $ interval = $ datetime1-> diff ($ datetime2 ); $ 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'); // returns $ time for the total days of two time differences ;} # use instance $ sss = diffDate ('2017-12-25 12:30:30 ', '2017-12-26 15:00:00'); print_r ($ sss ); # output Array ([y] => 00 [m] => 0 [d] => 1 [h] => 02 [I] => 29 [s] => 30 [a] => 1)

The second method is implemented using the function (still being tested ....)

// Calculate the difference between the two time ranges (hour, minute, and second) $ aaa = strtotime ('2017-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 preceding section describes how many years, months, and days are separated by two dates in php computing, including related content. I hope my friends who are interested in the PHP Tutorial will be helpful.

Related Article

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.