Programmer-PHP calculates the month of the difference between two years, and note that I only need the month, such as "2013-07-03" "2014-03-12"

Source: Internet
Author: User
PHP calculates the difference between two years of the month, note that I only months, such as "2013-07-03" "2014-03-12", they are 8 months apart, then your function must return 8, pay attention to the code redundancy

Reply content:

PHP calculates the difference between two years of the month, note that I only months, such as "2013-07-03" "2014-03-12", they are 8 months apart, then your function must return 8, pay attention to the code redundancy

The previous answer did not consider the year difference (but the main emphasis on the month only), here to fill @ 2014-03-27 10:06:25:

$time_begin   = strtotime("2012-07-03");$time_end     = strtotime("2014-03-12");$time_differ  = $time_end - $time_begin;$year_differ  = date('Y', $time_differ);$month_differ = date('m', $time_differ);$result       = 12*intval($year_differ-1970)+intval($month_differ)-1;
$time_begin   = strtotime("2013-07-03");$time_end     = strtotime("2014-03-12");$time_differ  = $time_end - $time_begin;$month_differ = date('m', $time_differ);$result       = intval($month_differ)-1;

The pro-test is feasible.

    $d1 = new DateTime('2013-07-03');    $d2 = new DateTime('2014-03-12');    $diff=$d2->diff($d1);    echo ($diff->y*12)+$diff->m;
  • 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.