Calculate days in months between two dates with PHP










2















I have a period with startdate of 2016-12-26 and end date 2017-03-04.



Now I would like to find out how many days in each months there is, from a given period. Expected output from the above period dates (array):



2016-12: 5
2017-01: 31
2017-02: 28
2017-03: 4


How can I accomplish this cleanest way? I have tried to:



  1. first looking at the period_start, get the days = 26 and

  2. find out the start/end dates of the months between 2016-12 and 2017-03, to then calculate the days here (31 respectively 28 in february)

  3. then finally calculating the 4 days in 2017-03.

But is there any cleaner/better way?










share|improve this question
























  • Check this - stackoverflow.com/questions/2040560/…

    – Suresh
    Apr 24 '17 at 9:24






  • 2





    if you start from 2016-12-26 then 2016-12 would be 5 not 26

    – Niklesh Raut
    Apr 24 '17 at 9:30












  • yes exactly i i also wants to know how your desired days are calculated ?

    – Bunker Boy
    Apr 24 '17 at 9:37






  • 1





    just count the seconds between the dates then convert it to days

    – Gert
    Apr 24 '17 at 9:37











  • @Niklesh This is very true. My bad, 2016-12 should be 5

    – Karem
    Apr 24 '17 at 10:41
















2















I have a period with startdate of 2016-12-26 and end date 2017-03-04.



Now I would like to find out how many days in each months there is, from a given period. Expected output from the above period dates (array):



2016-12: 5
2017-01: 31
2017-02: 28
2017-03: 4


How can I accomplish this cleanest way? I have tried to:



  1. first looking at the period_start, get the days = 26 and

  2. find out the start/end dates of the months between 2016-12 and 2017-03, to then calculate the days here (31 respectively 28 in february)

  3. then finally calculating the 4 days in 2017-03.

But is there any cleaner/better way?










share|improve this question
























  • Check this - stackoverflow.com/questions/2040560/…

    – Suresh
    Apr 24 '17 at 9:24






  • 2





    if you start from 2016-12-26 then 2016-12 would be 5 not 26

    – Niklesh Raut
    Apr 24 '17 at 9:30












  • yes exactly i i also wants to know how your desired days are calculated ?

    – Bunker Boy
    Apr 24 '17 at 9:37






  • 1





    just count the seconds between the dates then convert it to days

    – Gert
    Apr 24 '17 at 9:37











  • @Niklesh This is very true. My bad, 2016-12 should be 5

    – Karem
    Apr 24 '17 at 10:41














2












2








2








I have a period with startdate of 2016-12-26 and end date 2017-03-04.



Now I would like to find out how many days in each months there is, from a given period. Expected output from the above period dates (array):



2016-12: 5
2017-01: 31
2017-02: 28
2017-03: 4


How can I accomplish this cleanest way? I have tried to:



  1. first looking at the period_start, get the days = 26 and

  2. find out the start/end dates of the months between 2016-12 and 2017-03, to then calculate the days here (31 respectively 28 in february)

  3. then finally calculating the 4 days in 2017-03.

But is there any cleaner/better way?










share|improve this question
















I have a period with startdate of 2016-12-26 and end date 2017-03-04.



Now I would like to find out how many days in each months there is, from a given period. Expected output from the above period dates (array):



2016-12: 5
2017-01: 31
2017-02: 28
2017-03: 4


How can I accomplish this cleanest way? I have tried to:



  1. first looking at the period_start, get the days = 26 and

  2. find out the start/end dates of the months between 2016-12 and 2017-03, to then calculate the days here (31 respectively 28 in february)

  3. then finally calculating the 4 days in 2017-03.

But is there any cleaner/better way?







php






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 24 '17 at 10:41







Karem

















asked Apr 24 '17 at 9:21









KaremKarem

5,98463152258




5,98463152258












  • Check this - stackoverflow.com/questions/2040560/…

    – Suresh
    Apr 24 '17 at 9:24






  • 2





    if you start from 2016-12-26 then 2016-12 would be 5 not 26

    – Niklesh Raut
    Apr 24 '17 at 9:30












  • yes exactly i i also wants to know how your desired days are calculated ?

    – Bunker Boy
    Apr 24 '17 at 9:37






  • 1





    just count the seconds between the dates then convert it to days

    – Gert
    Apr 24 '17 at 9:37











  • @Niklesh This is very true. My bad, 2016-12 should be 5

    – Karem
    Apr 24 '17 at 10:41


















  • Check this - stackoverflow.com/questions/2040560/…

    – Suresh
    Apr 24 '17 at 9:24






  • 2





    if you start from 2016-12-26 then 2016-12 would be 5 not 26

    – Niklesh Raut
    Apr 24 '17 at 9:30












  • yes exactly i i also wants to know how your desired days are calculated ?

    – Bunker Boy
    Apr 24 '17 at 9:37






  • 1





    just count the seconds between the dates then convert it to days

    – Gert
    Apr 24 '17 at 9:37











  • @Niklesh This is very true. My bad, 2016-12 should be 5

    – Karem
    Apr 24 '17 at 10:41

















Check this - stackoverflow.com/questions/2040560/…

– Suresh
Apr 24 '17 at 9:24





Check this - stackoverflow.com/questions/2040560/…

– Suresh
Apr 24 '17 at 9:24




2




2





if you start from 2016-12-26 then 2016-12 would be 5 not 26

– Niklesh Raut
Apr 24 '17 at 9:30






if you start from 2016-12-26 then 2016-12 would be 5 not 26

– Niklesh Raut
Apr 24 '17 at 9:30














yes exactly i i also wants to know how your desired days are calculated ?

– Bunker Boy
Apr 24 '17 at 9:37





yes exactly i i also wants to know how your desired days are calculated ?

– Bunker Boy
Apr 24 '17 at 9:37




1




1





just count the seconds between the dates then convert it to days

– Gert
Apr 24 '17 at 9:37





just count the seconds between the dates then convert it to days

– Gert
Apr 24 '17 at 9:37













@Niklesh This is very true. My bad, 2016-12 should be 5

– Karem
Apr 24 '17 at 10:41






@Niklesh This is very true. My bad, 2016-12 should be 5

– Karem
Apr 24 '17 at 10:41













5 Answers
5






active

oldest

votes


















2














This can be achieved easily using the DateTime class. Create the objects, and use DateTime::diff() on them, then use the days property.



$start = new DateTime("2016-12-26");
$end = new DateTime("2017-03-04");

echo $start->diff($end)->days; // Output: 68


Live demo



  • http://php.net/datetime.construct





share|improve this answer























  • Thanks, but I look for a solution that breaks down the days into months rather than the sum for the whole period.

    – Karem
    Apr 24 '17 at 10:42


















1














@Karem hope this logic will help you, this is working case for all your conditions please try this below one:



<?php
$startDate = '2016-12-26';
$endDate = '2017-03-04';
$varDate = $startDate;
while($varDate < $endDate)
$d = date('d', strtotime($varDate));
$Y = date('Y', strtotime($varDate));
$m = date('m', strtotime($varDate));
$days = cal_days_in_month(CAL_GREGORIAN,$m,$Y);
$time = strtotime($varDate);
if($varDate == $startDate)
$time = strtotime(date('Y-m-01', $time));
$days = $days - $d;

else if(date("Y-m", strtotime($varDate)) == date("Y-m", strtotime($endDate)))
$days = date("j", strtotime($endDate));

echo date('Y-m', strtotime($varDate)). ": ".$days."<br>";
$varDate = date('Y-m-d', strtotime("+1 month", $time));






share|improve this answer

























  • Thanks your for this! The per month is correct, but amount of days is wrong (26 should be 5, sorry for the mistake in question). So it should calculate how many days in there is in the month from 26th december (5). Also the static if($startDate == '2016-12-26' || $startDate == '2017-03-01') seems like a "quickfix" more than a proper solution that would work for any date periods

    – Karem
    Apr 24 '17 at 10:52











  • @Karem i have asked in the comment box, i thought it should be 26 ok let me update my answer

    – Bunker Boy
    Apr 24 '17 at 10:54






  • 1





    @Karem please check my updated answer, this is according to you

    – Bunker Boy
    Apr 24 '17 at 11:02











  • Always try to add live demo : eval.in/781778

    – Niklesh Raut
    Apr 24 '17 at 12:47












  • @ Niklesh i ll remember (y)

    – Bunker Boy
    Apr 24 '17 at 12:48


















1














This is long but easy to understand that how to achieve you your goal



<?php
function getMonthDays($start,$end)
if($start < $end)
$start_time = strtotime($start);
$last_day_of_start = strtotime(date("Y-m-t",$start_time));
$start_month_days = ($last_day_of_start - $start_time)/(60*60*24);
echo date("Y-m",$start_time).": ".$start_month_days."n";
$days = "";
$start = date("Y-m-d", strtotime("+1 month", $start_time));
$start_time = strtotime($start);
while($start < $end)
$month = date("m",$start_time);
$year = date("Y",$start_time);
$days = date('t', mktime(0, 0, 0, $month, 1, $year));
echo date("Y-m",$start_time).": ".$days."n";
$start = date("Y-m-d", strtotime("+1 month", $start_time));
$start_time = strtotime($start);

echo date("Y-m",strtotime($end)).": ".date("d",strtotime($end))."n";

else
echo "Wrong Input";


getMonthDays('2016-12-26','2017-03-04');
?>


live demo : https://eval.in/781724



Function returns array : https://eval.in/781741






share|improve this answer

























  • I like this solution. Thank you for contribution. But why +1 month? Case: if period_start is the same as period_end, it should be 0 days. Case 2: getMonthDays('2016-12-26','2016-12-27'); fails

    – Karem
    Apr 25 '17 at 9:09












  • Added both conditions also : eval.in/782363.

    – Niklesh Raut
    Apr 25 '17 at 9:51











  • Great, thanks for this! Makes sense. Any way the day can be counted aswell? getMonthDays('2016-12-26','2016-12-27'); should return 2, not 1.

    – Karem
    Apr 25 '17 at 11:44


















0














<?php
$d1 = strtotime('2016-12-26');
$d2 = strtotime('2017-03-04');
echo floor(($d2 - $d1)/(60*60*24));
?>





share|improve this answer























  • Whilst this code snippet is welcome, and may provide some help, it would be greatly improved if it included an explanation of how it addresses the question. Without that, your answer has much less educational value - remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply. In particular, how does this code produce a line for each month?

    – Toby Speight
    Apr 24 '17 at 11:48


















0














i used Carbon (https://carbon.nesbot.com/docs/) but you can do it with any other time lib.



$startDate = Carbon::createFromFormat('!Y-m-d', '2017-01-11');;
$endDate = Carbon::createFromFormat('!Y-m-d', '2018-11-13');;

$diffInMonths = $endDate->diffInMonths($startDate);

for ($i = 0; $i <= $diffInMonths; $i++)
$start = $i == 0 ? $startDate->copy()->addMonth($i) : $startDate->copy()->addMonth($i)->firstOfMonth();
$end = $diffInMonths == $i ? $endDate->copy() : $start->copy()->endOfMonth();

echo $end->format('Y-m') . ' ' . ($end->diffInDays($start) + 1) . PHP_EOL;






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43584254%2fcalculate-days-in-months-between-two-dates-with-php%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    5 Answers
    5






    active

    oldest

    votes








    5 Answers
    5






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    This can be achieved easily using the DateTime class. Create the objects, and use DateTime::diff() on them, then use the days property.



    $start = new DateTime("2016-12-26");
    $end = new DateTime("2017-03-04");

    echo $start->diff($end)->days; // Output: 68


    Live demo



    • http://php.net/datetime.construct





    share|improve this answer























    • Thanks, but I look for a solution that breaks down the days into months rather than the sum for the whole period.

      – Karem
      Apr 24 '17 at 10:42















    2














    This can be achieved easily using the DateTime class. Create the objects, and use DateTime::diff() on them, then use the days property.



    $start = new DateTime("2016-12-26");
    $end = new DateTime("2017-03-04");

    echo $start->diff($end)->days; // Output: 68


    Live demo



    • http://php.net/datetime.construct





    share|improve this answer























    • Thanks, but I look for a solution that breaks down the days into months rather than the sum for the whole period.

      – Karem
      Apr 24 '17 at 10:42













    2












    2








    2







    This can be achieved easily using the DateTime class. Create the objects, and use DateTime::diff() on them, then use the days property.



    $start = new DateTime("2016-12-26");
    $end = new DateTime("2017-03-04");

    echo $start->diff($end)->days; // Output: 68


    Live demo



    • http://php.net/datetime.construct





    share|improve this answer













    This can be achieved easily using the DateTime class. Create the objects, and use DateTime::diff() on them, then use the days property.



    $start = new DateTime("2016-12-26");
    $end = new DateTime("2017-03-04");

    echo $start->diff($end)->days; // Output: 68


    Live demo



    • http://php.net/datetime.construct






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Apr 24 '17 at 9:42









    QirelQirel

    10.7k62540




    10.7k62540












    • Thanks, but I look for a solution that breaks down the days into months rather than the sum for the whole period.

      – Karem
      Apr 24 '17 at 10:42

















    • Thanks, but I look for a solution that breaks down the days into months rather than the sum for the whole period.

      – Karem
      Apr 24 '17 at 10:42
















    Thanks, but I look for a solution that breaks down the days into months rather than the sum for the whole period.

    – Karem
    Apr 24 '17 at 10:42





    Thanks, but I look for a solution that breaks down the days into months rather than the sum for the whole period.

    – Karem
    Apr 24 '17 at 10:42













    1














    @Karem hope this logic will help you, this is working case for all your conditions please try this below one:



    <?php
    $startDate = '2016-12-26';
    $endDate = '2017-03-04';
    $varDate = $startDate;
    while($varDate < $endDate)
    $d = date('d', strtotime($varDate));
    $Y = date('Y', strtotime($varDate));
    $m = date('m', strtotime($varDate));
    $days = cal_days_in_month(CAL_GREGORIAN,$m,$Y);
    $time = strtotime($varDate);
    if($varDate == $startDate)
    $time = strtotime(date('Y-m-01', $time));
    $days = $days - $d;

    else if(date("Y-m", strtotime($varDate)) == date("Y-m", strtotime($endDate)))
    $days = date("j", strtotime($endDate));

    echo date('Y-m', strtotime($varDate)). ": ".$days."<br>";
    $varDate = date('Y-m-d', strtotime("+1 month", $time));






    share|improve this answer

























    • Thanks your for this! The per month is correct, but amount of days is wrong (26 should be 5, sorry for the mistake in question). So it should calculate how many days in there is in the month from 26th december (5). Also the static if($startDate == '2016-12-26' || $startDate == '2017-03-01') seems like a "quickfix" more than a proper solution that would work for any date periods

      – Karem
      Apr 24 '17 at 10:52











    • @Karem i have asked in the comment box, i thought it should be 26 ok let me update my answer

      – Bunker Boy
      Apr 24 '17 at 10:54






    • 1





      @Karem please check my updated answer, this is according to you

      – Bunker Boy
      Apr 24 '17 at 11:02











    • Always try to add live demo : eval.in/781778

      – Niklesh Raut
      Apr 24 '17 at 12:47












    • @ Niklesh i ll remember (y)

      – Bunker Boy
      Apr 24 '17 at 12:48















    1














    @Karem hope this logic will help you, this is working case for all your conditions please try this below one:



    <?php
    $startDate = '2016-12-26';
    $endDate = '2017-03-04';
    $varDate = $startDate;
    while($varDate < $endDate)
    $d = date('d', strtotime($varDate));
    $Y = date('Y', strtotime($varDate));
    $m = date('m', strtotime($varDate));
    $days = cal_days_in_month(CAL_GREGORIAN,$m,$Y);
    $time = strtotime($varDate);
    if($varDate == $startDate)
    $time = strtotime(date('Y-m-01', $time));
    $days = $days - $d;

    else if(date("Y-m", strtotime($varDate)) == date("Y-m", strtotime($endDate)))
    $days = date("j", strtotime($endDate));

    echo date('Y-m', strtotime($varDate)). ": ".$days."<br>";
    $varDate = date('Y-m-d', strtotime("+1 month", $time));






    share|improve this answer

























    • Thanks your for this! The per month is correct, but amount of days is wrong (26 should be 5, sorry for the mistake in question). So it should calculate how many days in there is in the month from 26th december (5). Also the static if($startDate == '2016-12-26' || $startDate == '2017-03-01') seems like a "quickfix" more than a proper solution that would work for any date periods

      – Karem
      Apr 24 '17 at 10:52











    • @Karem i have asked in the comment box, i thought it should be 26 ok let me update my answer

      – Bunker Boy
      Apr 24 '17 at 10:54






    • 1





      @Karem please check my updated answer, this is according to you

      – Bunker Boy
      Apr 24 '17 at 11:02











    • Always try to add live demo : eval.in/781778

      – Niklesh Raut
      Apr 24 '17 at 12:47












    • @ Niklesh i ll remember (y)

      – Bunker Boy
      Apr 24 '17 at 12:48













    1












    1








    1







    @Karem hope this logic will help you, this is working case for all your conditions please try this below one:



    <?php
    $startDate = '2016-12-26';
    $endDate = '2017-03-04';
    $varDate = $startDate;
    while($varDate < $endDate)
    $d = date('d', strtotime($varDate));
    $Y = date('Y', strtotime($varDate));
    $m = date('m', strtotime($varDate));
    $days = cal_days_in_month(CAL_GREGORIAN,$m,$Y);
    $time = strtotime($varDate);
    if($varDate == $startDate)
    $time = strtotime(date('Y-m-01', $time));
    $days = $days - $d;

    else if(date("Y-m", strtotime($varDate)) == date("Y-m", strtotime($endDate)))
    $days = date("j", strtotime($endDate));

    echo date('Y-m', strtotime($varDate)). ": ".$days."<br>";
    $varDate = date('Y-m-d', strtotime("+1 month", $time));






    share|improve this answer















    @Karem hope this logic will help you, this is working case for all your conditions please try this below one:



    <?php
    $startDate = '2016-12-26';
    $endDate = '2017-03-04';
    $varDate = $startDate;
    while($varDate < $endDate)
    $d = date('d', strtotime($varDate));
    $Y = date('Y', strtotime($varDate));
    $m = date('m', strtotime($varDate));
    $days = cal_days_in_month(CAL_GREGORIAN,$m,$Y);
    $time = strtotime($varDate);
    if($varDate == $startDate)
    $time = strtotime(date('Y-m-01', $time));
    $days = $days - $d;

    else if(date("Y-m", strtotime($varDate)) == date("Y-m", strtotime($endDate)))
    $days = date("j", strtotime($endDate));

    echo date('Y-m', strtotime($varDate)). ": ".$days."<br>";
    $varDate = date('Y-m-d', strtotime("+1 month", $time));







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Apr 24 '17 at 11:06

























    answered Apr 24 '17 at 10:23









    Bunker BoyBunker Boy

    1,5051928




    1,5051928












    • Thanks your for this! The per month is correct, but amount of days is wrong (26 should be 5, sorry for the mistake in question). So it should calculate how many days in there is in the month from 26th december (5). Also the static if($startDate == '2016-12-26' || $startDate == '2017-03-01') seems like a "quickfix" more than a proper solution that would work for any date periods

      – Karem
      Apr 24 '17 at 10:52











    • @Karem i have asked in the comment box, i thought it should be 26 ok let me update my answer

      – Bunker Boy
      Apr 24 '17 at 10:54






    • 1





      @Karem please check my updated answer, this is according to you

      – Bunker Boy
      Apr 24 '17 at 11:02











    • Always try to add live demo : eval.in/781778

      – Niklesh Raut
      Apr 24 '17 at 12:47












    • @ Niklesh i ll remember (y)

      – Bunker Boy
      Apr 24 '17 at 12:48

















    • Thanks your for this! The per month is correct, but amount of days is wrong (26 should be 5, sorry for the mistake in question). So it should calculate how many days in there is in the month from 26th december (5). Also the static if($startDate == '2016-12-26' || $startDate == '2017-03-01') seems like a "quickfix" more than a proper solution that would work for any date periods

      – Karem
      Apr 24 '17 at 10:52











    • @Karem i have asked in the comment box, i thought it should be 26 ok let me update my answer

      – Bunker Boy
      Apr 24 '17 at 10:54






    • 1





      @Karem please check my updated answer, this is according to you

      – Bunker Boy
      Apr 24 '17 at 11:02











    • Always try to add live demo : eval.in/781778

      – Niklesh Raut
      Apr 24 '17 at 12:47












    • @ Niklesh i ll remember (y)

      – Bunker Boy
      Apr 24 '17 at 12:48
















    Thanks your for this! The per month is correct, but amount of days is wrong (26 should be 5, sorry for the mistake in question). So it should calculate how many days in there is in the month from 26th december (5). Also the static if($startDate == '2016-12-26' || $startDate == '2017-03-01') seems like a "quickfix" more than a proper solution that would work for any date periods

    – Karem
    Apr 24 '17 at 10:52





    Thanks your for this! The per month is correct, but amount of days is wrong (26 should be 5, sorry for the mistake in question). So it should calculate how many days in there is in the month from 26th december (5). Also the static if($startDate == '2016-12-26' || $startDate == '2017-03-01') seems like a "quickfix" more than a proper solution that would work for any date periods

    – Karem
    Apr 24 '17 at 10:52













    @Karem i have asked in the comment box, i thought it should be 26 ok let me update my answer

    – Bunker Boy
    Apr 24 '17 at 10:54





    @Karem i have asked in the comment box, i thought it should be 26 ok let me update my answer

    – Bunker Boy
    Apr 24 '17 at 10:54




    1




    1





    @Karem please check my updated answer, this is according to you

    – Bunker Boy
    Apr 24 '17 at 11:02





    @Karem please check my updated answer, this is according to you

    – Bunker Boy
    Apr 24 '17 at 11:02













    Always try to add live demo : eval.in/781778

    – Niklesh Raut
    Apr 24 '17 at 12:47






    Always try to add live demo : eval.in/781778

    – Niklesh Raut
    Apr 24 '17 at 12:47














    @ Niklesh i ll remember (y)

    – Bunker Boy
    Apr 24 '17 at 12:48





    @ Niklesh i ll remember (y)

    – Bunker Boy
    Apr 24 '17 at 12:48











    1














    This is long but easy to understand that how to achieve you your goal



    <?php
    function getMonthDays($start,$end)
    if($start < $end)
    $start_time = strtotime($start);
    $last_day_of_start = strtotime(date("Y-m-t",$start_time));
    $start_month_days = ($last_day_of_start - $start_time)/(60*60*24);
    echo date("Y-m",$start_time).": ".$start_month_days."n";
    $days = "";
    $start = date("Y-m-d", strtotime("+1 month", $start_time));
    $start_time = strtotime($start);
    while($start < $end)
    $month = date("m",$start_time);
    $year = date("Y",$start_time);
    $days = date('t', mktime(0, 0, 0, $month, 1, $year));
    echo date("Y-m",$start_time).": ".$days."n";
    $start = date("Y-m-d", strtotime("+1 month", $start_time));
    $start_time = strtotime($start);

    echo date("Y-m",strtotime($end)).": ".date("d",strtotime($end))."n";

    else
    echo "Wrong Input";


    getMonthDays('2016-12-26','2017-03-04');
    ?>


    live demo : https://eval.in/781724



    Function returns array : https://eval.in/781741






    share|improve this answer

























    • I like this solution. Thank you for contribution. But why +1 month? Case: if period_start is the same as period_end, it should be 0 days. Case 2: getMonthDays('2016-12-26','2016-12-27'); fails

      – Karem
      Apr 25 '17 at 9:09












    • Added both conditions also : eval.in/782363.

      – Niklesh Raut
      Apr 25 '17 at 9:51











    • Great, thanks for this! Makes sense. Any way the day can be counted aswell? getMonthDays('2016-12-26','2016-12-27'); should return 2, not 1.

      – Karem
      Apr 25 '17 at 11:44















    1














    This is long but easy to understand that how to achieve you your goal



    <?php
    function getMonthDays($start,$end)
    if($start < $end)
    $start_time = strtotime($start);
    $last_day_of_start = strtotime(date("Y-m-t",$start_time));
    $start_month_days = ($last_day_of_start - $start_time)/(60*60*24);
    echo date("Y-m",$start_time).": ".$start_month_days."n";
    $days = "";
    $start = date("Y-m-d", strtotime("+1 month", $start_time));
    $start_time = strtotime($start);
    while($start < $end)
    $month = date("m",$start_time);
    $year = date("Y",$start_time);
    $days = date('t', mktime(0, 0, 0, $month, 1, $year));
    echo date("Y-m",$start_time).": ".$days."n";
    $start = date("Y-m-d", strtotime("+1 month", $start_time));
    $start_time = strtotime($start);

    echo date("Y-m",strtotime($end)).": ".date("d",strtotime($end))."n";

    else
    echo "Wrong Input";


    getMonthDays('2016-12-26','2017-03-04');
    ?>


    live demo : https://eval.in/781724



    Function returns array : https://eval.in/781741






    share|improve this answer

























    • I like this solution. Thank you for contribution. But why +1 month? Case: if period_start is the same as period_end, it should be 0 days. Case 2: getMonthDays('2016-12-26','2016-12-27'); fails

      – Karem
      Apr 25 '17 at 9:09












    • Added both conditions also : eval.in/782363.

      – Niklesh Raut
      Apr 25 '17 at 9:51











    • Great, thanks for this! Makes sense. Any way the day can be counted aswell? getMonthDays('2016-12-26','2016-12-27'); should return 2, not 1.

      – Karem
      Apr 25 '17 at 11:44













    1












    1








    1







    This is long but easy to understand that how to achieve you your goal



    <?php
    function getMonthDays($start,$end)
    if($start < $end)
    $start_time = strtotime($start);
    $last_day_of_start = strtotime(date("Y-m-t",$start_time));
    $start_month_days = ($last_day_of_start - $start_time)/(60*60*24);
    echo date("Y-m",$start_time).": ".$start_month_days."n";
    $days = "";
    $start = date("Y-m-d", strtotime("+1 month", $start_time));
    $start_time = strtotime($start);
    while($start < $end)
    $month = date("m",$start_time);
    $year = date("Y",$start_time);
    $days = date('t', mktime(0, 0, 0, $month, 1, $year));
    echo date("Y-m",$start_time).": ".$days."n";
    $start = date("Y-m-d", strtotime("+1 month", $start_time));
    $start_time = strtotime($start);

    echo date("Y-m",strtotime($end)).": ".date("d",strtotime($end))."n";

    else
    echo "Wrong Input";


    getMonthDays('2016-12-26','2017-03-04');
    ?>


    live demo : https://eval.in/781724



    Function returns array : https://eval.in/781741






    share|improve this answer















    This is long but easy to understand that how to achieve you your goal



    <?php
    function getMonthDays($start,$end)
    if($start < $end)
    $start_time = strtotime($start);
    $last_day_of_start = strtotime(date("Y-m-t",$start_time));
    $start_month_days = ($last_day_of_start - $start_time)/(60*60*24);
    echo date("Y-m",$start_time).": ".$start_month_days."n";
    $days = "";
    $start = date("Y-m-d", strtotime("+1 month", $start_time));
    $start_time = strtotime($start);
    while($start < $end)
    $month = date("m",$start_time);
    $year = date("Y",$start_time);
    $days = date('t', mktime(0, 0, 0, $month, 1, $year));
    echo date("Y-m",$start_time).": ".$days."n";
    $start = date("Y-m-d", strtotime("+1 month", $start_time));
    $start_time = strtotime($start);

    echo date("Y-m",strtotime($end)).": ".date("d",strtotime($end))."n";

    else
    echo "Wrong Input";


    getMonthDays('2016-12-26','2017-03-04');
    ?>


    live demo : https://eval.in/781724



    Function returns array : https://eval.in/781741







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Apr 24 '17 at 11:55

























    answered Apr 24 '17 at 11:45









    Niklesh RautNiklesh Raut

    19.5k43069




    19.5k43069












    • I like this solution. Thank you for contribution. But why +1 month? Case: if period_start is the same as period_end, it should be 0 days. Case 2: getMonthDays('2016-12-26','2016-12-27'); fails

      – Karem
      Apr 25 '17 at 9:09












    • Added both conditions also : eval.in/782363.

      – Niklesh Raut
      Apr 25 '17 at 9:51











    • Great, thanks for this! Makes sense. Any way the day can be counted aswell? getMonthDays('2016-12-26','2016-12-27'); should return 2, not 1.

      – Karem
      Apr 25 '17 at 11:44

















    • I like this solution. Thank you for contribution. But why +1 month? Case: if period_start is the same as period_end, it should be 0 days. Case 2: getMonthDays('2016-12-26','2016-12-27'); fails

      – Karem
      Apr 25 '17 at 9:09












    • Added both conditions also : eval.in/782363.

      – Niklesh Raut
      Apr 25 '17 at 9:51











    • Great, thanks for this! Makes sense. Any way the day can be counted aswell? getMonthDays('2016-12-26','2016-12-27'); should return 2, not 1.

      – Karem
      Apr 25 '17 at 11:44
















    I like this solution. Thank you for contribution. But why +1 month? Case: if period_start is the same as period_end, it should be 0 days. Case 2: getMonthDays('2016-12-26','2016-12-27'); fails

    – Karem
    Apr 25 '17 at 9:09






    I like this solution. Thank you for contribution. But why +1 month? Case: if period_start is the same as period_end, it should be 0 days. Case 2: getMonthDays('2016-12-26','2016-12-27'); fails

    – Karem
    Apr 25 '17 at 9:09














    Added both conditions also : eval.in/782363.

    – Niklesh Raut
    Apr 25 '17 at 9:51





    Added both conditions also : eval.in/782363.

    – Niklesh Raut
    Apr 25 '17 at 9:51













    Great, thanks for this! Makes sense. Any way the day can be counted aswell? getMonthDays('2016-12-26','2016-12-27'); should return 2, not 1.

    – Karem
    Apr 25 '17 at 11:44





    Great, thanks for this! Makes sense. Any way the day can be counted aswell? getMonthDays('2016-12-26','2016-12-27'); should return 2, not 1.

    – Karem
    Apr 25 '17 at 11:44











    0














    <?php
    $d1 = strtotime('2016-12-26');
    $d2 = strtotime('2017-03-04');
    echo floor(($d2 - $d1)/(60*60*24));
    ?>





    share|improve this answer























    • Whilst this code snippet is welcome, and may provide some help, it would be greatly improved if it included an explanation of how it addresses the question. Without that, your answer has much less educational value - remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply. In particular, how does this code produce a line for each month?

      – Toby Speight
      Apr 24 '17 at 11:48















    0














    <?php
    $d1 = strtotime('2016-12-26');
    $d2 = strtotime('2017-03-04');
    echo floor(($d2 - $d1)/(60*60*24));
    ?>





    share|improve this answer























    • Whilst this code snippet is welcome, and may provide some help, it would be greatly improved if it included an explanation of how it addresses the question. Without that, your answer has much less educational value - remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply. In particular, how does this code produce a line for each month?

      – Toby Speight
      Apr 24 '17 at 11:48













    0












    0








    0







    <?php
    $d1 = strtotime('2016-12-26');
    $d2 = strtotime('2017-03-04');
    echo floor(($d2 - $d1)/(60*60*24));
    ?>





    share|improve this answer













    <?php
    $d1 = strtotime('2016-12-26');
    $d2 = strtotime('2017-03-04');
    echo floor(($d2 - $d1)/(60*60*24));
    ?>






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Apr 24 '17 at 9:34









    Gopi ChandGopi Chand

    1146




    1146












    • Whilst this code snippet is welcome, and may provide some help, it would be greatly improved if it included an explanation of how it addresses the question. Without that, your answer has much less educational value - remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply. In particular, how does this code produce a line for each month?

      – Toby Speight
      Apr 24 '17 at 11:48

















    • Whilst this code snippet is welcome, and may provide some help, it would be greatly improved if it included an explanation of how it addresses the question. Without that, your answer has much less educational value - remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply. In particular, how does this code produce a line for each month?

      – Toby Speight
      Apr 24 '17 at 11:48
















    Whilst this code snippet is welcome, and may provide some help, it would be greatly improved if it included an explanation of how it addresses the question. Without that, your answer has much less educational value - remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply. In particular, how does this code produce a line for each month?

    – Toby Speight
    Apr 24 '17 at 11:48





    Whilst this code snippet is welcome, and may provide some help, it would be greatly improved if it included an explanation of how it addresses the question. Without that, your answer has much less educational value - remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply. In particular, how does this code produce a line for each month?

    – Toby Speight
    Apr 24 '17 at 11:48











    0














    i used Carbon (https://carbon.nesbot.com/docs/) but you can do it with any other time lib.



    $startDate = Carbon::createFromFormat('!Y-m-d', '2017-01-11');;
    $endDate = Carbon::createFromFormat('!Y-m-d', '2018-11-13');;

    $diffInMonths = $endDate->diffInMonths($startDate);

    for ($i = 0; $i <= $diffInMonths; $i++)
    $start = $i == 0 ? $startDate->copy()->addMonth($i) : $startDate->copy()->addMonth($i)->firstOfMonth();
    $end = $diffInMonths == $i ? $endDate->copy() : $start->copy()->endOfMonth();

    echo $end->format('Y-m') . ' ' . ($end->diffInDays($start) + 1) . PHP_EOL;






    share|improve this answer



























      0














      i used Carbon (https://carbon.nesbot.com/docs/) but you can do it with any other time lib.



      $startDate = Carbon::createFromFormat('!Y-m-d', '2017-01-11');;
      $endDate = Carbon::createFromFormat('!Y-m-d', '2018-11-13');;

      $diffInMonths = $endDate->diffInMonths($startDate);

      for ($i = 0; $i <= $diffInMonths; $i++)
      $start = $i == 0 ? $startDate->copy()->addMonth($i) : $startDate->copy()->addMonth($i)->firstOfMonth();
      $end = $diffInMonths == $i ? $endDate->copy() : $start->copy()->endOfMonth();

      echo $end->format('Y-m') . ' ' . ($end->diffInDays($start) + 1) . PHP_EOL;






      share|improve this answer

























        0












        0








        0







        i used Carbon (https://carbon.nesbot.com/docs/) but you can do it with any other time lib.



        $startDate = Carbon::createFromFormat('!Y-m-d', '2017-01-11');;
        $endDate = Carbon::createFromFormat('!Y-m-d', '2018-11-13');;

        $diffInMonths = $endDate->diffInMonths($startDate);

        for ($i = 0; $i <= $diffInMonths; $i++)
        $start = $i == 0 ? $startDate->copy()->addMonth($i) : $startDate->copy()->addMonth($i)->firstOfMonth();
        $end = $diffInMonths == $i ? $endDate->copy() : $start->copy()->endOfMonth();

        echo $end->format('Y-m') . ' ' . ($end->diffInDays($start) + 1) . PHP_EOL;






        share|improve this answer













        i used Carbon (https://carbon.nesbot.com/docs/) but you can do it with any other time lib.



        $startDate = Carbon::createFromFormat('!Y-m-d', '2017-01-11');;
        $endDate = Carbon::createFromFormat('!Y-m-d', '2018-11-13');;

        $diffInMonths = $endDate->diffInMonths($startDate);

        for ($i = 0; $i <= $diffInMonths; $i++)
        $start = $i == 0 ? $startDate->copy()->addMonth($i) : $startDate->copy()->addMonth($i)->firstOfMonth();
        $end = $diffInMonths == $i ? $endDate->copy() : $start->copy()->endOfMonth();

        echo $end->format('Y-m') . ' ' . ($end->diffInDays($start) + 1) . PHP_EOL;







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 12:20









        Michał ChwedkowskiMichał Chwedkowski

        1




        1



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43584254%2fcalculate-days-in-months-between-two-dates-with-php%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            What does pagestruct do in Eviews?

            Dutch intervention in Lombok and Karangasem

            Channel Islands