获取天数并确定两个日期之间的哪个月

时间:2021-07-29 14:18:35

I know it is very crazy, but I am still asking for your solution.

我知道这很疯狂,但我仍然在寻求你的解决方案。

Now, I have two dates: let say start date: 2017-02-01 and end date: 2017-04-01.

现在,我有两个日期:让我们说开始日期:2017-02-01和结束日期:2017-04-01。

If I compare the end date with the start date, of course I would get 60 days (includes one day of 2017-02-01 for itself).

如果我将结束日期与开始日期进行比较,我当然会得到60天(其中包括2017-02-01的一天)。

Assume I don't know what month and how many days do the months have between these 2 dates. Erm...is there anyway to determine the days and month... clearly?

假设我不知道这两个日期之间的月份和月份是多少天。呃...无论如何要确定日期和月份......很清楚?

Something like this:

像这样的东西:

  1. 2017-02-01 => 2017-02-28 = 28 days AND this is February
  2. 2017-02-01 => 2017-02-28 = 28天,这是二月

  3. 2017-03-01 => 2017-03-31 = 31 days AND this is March
  4. 2017-03-01 => 2017-03-31 = 31天这是3月

  5. 2017-04-01 => 2017-04-01 = 1 day AND this is April
  6. 2017-04-01 => 2017-04-01 = 1天这是4月

I could only find the days between two dates in PHP, and of course it is easier than this one. I think this one already covers the array concept, and already beyond my IQ XD.

我只能在PHP中找到两个日期之间的日子,当然它比这个更容易。我认为这个已经涵盖了阵列概念,已经超出了我的IQ XD。

I need your help, prove yourselves by this solving this question, thank you so much! Much appreciated.

我需要你的帮助,通过解决这个问题证明你自己,非常感谢你!非常感激。

In simpler: What I wanted to find is -- I don't want to find the day between two dates, I wanna find what month and how many days for each month between these two dates.

更简单:我想要找到的是 - 我不想找到两个日期之间的那一天,我想找到这两个日期之间每月的月份和天数。

P/S: I don't have any code for this, because I don't even know how to start to code this.

P / S:我没有任何代码,因为我甚至不知道如何开始编码。

3 个解决方案

#1


2  

You can use date_diff function. date_diff function count the day between two date, so you can add 2 to count your date too..

您可以使用date_diff函数。 date_diff函数计算两个日期之间的日期,因此您可以添加2来计算您的日期..

$date1=date_create("2017-02-01");
$date2=date_create("2017-04-01");
$diff=date_diff($date1,$date2);

$res =  $diff->format("%R%a days");
echo ($res + 2);

#2


0  

PHP has some quite powerful DateTime classes and functions.

PHP有一些非常强大的DateTime类和函数。

<?php

$start = new DateTime('2017-05-01');
$end = new DateTime('2017-07-03');
$interval = new DateInterval('P1D');
$period = new DatePeriod($start, $interval, $end);

$output = ['daysTotal' => 0, 'months' => []];
$months = [];
foreach ($period as $dt) {
    if (!in_array($dt->format("F"), $months)) {
        $month = ['name' => $dt->format("F"), 'days' => cal_days_in_month(CAL_GREGORIAN, $dt->format("n"), $dt->format("Y"))];
        $output['months'][] = $month;
        $months[] = $dt->format("F");

    }
    $output['daysTotal']++;
}

var_dump($output);

This will output:

这将输出:

array(2) {
  ["daysTotal"]=>
  int(63)
  ["months"]=>
  array(3) {
    [0]=>
    array(2) {
      ["name"]=>
      string(3) "May"
      ["days"]=>
      int(31)
    }
    [1]=>
    array(2) {
      ["name"]=>
      string(4) "June"
      ["days"]=>
      int(30)
    }
    [2]=>
    array(2) {
      ["name"]=>
      string(4) "July"
      ["days"]=>
      int(31)
    }
  }
}

Note: If you do between 2017-05-01 and 2017-06-01, it will not register June in the months array.

注意:如果您在2017-05-01和2017-06-01之间进行操作,则不会在月份数组中注册6月。

#3


0  

just try this

试试吧

$from_date = "2017-02-01";
$to_date = "2017-04-01";
$start = $month = strtotime($from_date);
$tmp = cal_days_in_month(CAL_GREGORIAN,date('m',strtotime($to_date)),date('Y',strtotime($to_date)));
$end = strtotime(date('Y-m',strtotime($to_date)).'-'.$tmp);
while($month < $end){
    $days = cal_days_in_month(CAL_GREGORIAN,date('m',$month),date('Y',$month));
    if(date('m',$start) == date('m',$month)) {
        $days = $days - date('d',$start) + 1;
    } else if(date('m',$end) == date('m',$month)) {
        $days = date('d',strtotime($to_date));
    }
    echo "Days :".$days." Month :".date('F',$month);
    echo '<br>';
    $month = strtotime("+1 month", $month);
}

output will looks like below

输出将如下所示

Days :28 Month :February Days :31 Month :March Days :01 Month :April

#1


2  

You can use date_diff function. date_diff function count the day between two date, so you can add 2 to count your date too..

您可以使用date_diff函数。 date_diff函数计算两个日期之间的日期,因此您可以添加2来计算您的日期..

$date1=date_create("2017-02-01");
$date2=date_create("2017-04-01");
$diff=date_diff($date1,$date2);

$res =  $diff->format("%R%a days");
echo ($res + 2);

#2


0  

PHP has some quite powerful DateTime classes and functions.

PHP有一些非常强大的DateTime类和函数。

<?php

$start = new DateTime('2017-05-01');
$end = new DateTime('2017-07-03');
$interval = new DateInterval('P1D');
$period = new DatePeriod($start, $interval, $end);

$output = ['daysTotal' => 0, 'months' => []];
$months = [];
foreach ($period as $dt) {
    if (!in_array($dt->format("F"), $months)) {
        $month = ['name' => $dt->format("F"), 'days' => cal_days_in_month(CAL_GREGORIAN, $dt->format("n"), $dt->format("Y"))];
        $output['months'][] = $month;
        $months[] = $dt->format("F");

    }
    $output['daysTotal']++;
}

var_dump($output);

This will output:

这将输出:

array(2) {
  ["daysTotal"]=>
  int(63)
  ["months"]=>
  array(3) {
    [0]=>
    array(2) {
      ["name"]=>
      string(3) "May"
      ["days"]=>
      int(31)
    }
    [1]=>
    array(2) {
      ["name"]=>
      string(4) "June"
      ["days"]=>
      int(30)
    }
    [2]=>
    array(2) {
      ["name"]=>
      string(4) "July"
      ["days"]=>
      int(31)
    }
  }
}

Note: If you do between 2017-05-01 and 2017-06-01, it will not register June in the months array.

注意:如果您在2017-05-01和2017-06-01之间进行操作,则不会在月份数组中注册6月。

#3


0  

just try this

试试吧

$from_date = "2017-02-01";
$to_date = "2017-04-01";
$start = $month = strtotime($from_date);
$tmp = cal_days_in_month(CAL_GREGORIAN,date('m',strtotime($to_date)),date('Y',strtotime($to_date)));
$end = strtotime(date('Y-m',strtotime($to_date)).'-'.$tmp);
while($month < $end){
    $days = cal_days_in_month(CAL_GREGORIAN,date('m',$month),date('Y',$month));
    if(date('m',$start) == date('m',$month)) {
        $days = $days - date('d',$start) + 1;
    } else if(date('m',$end) == date('m',$month)) {
        $days = date('d',strtotime($to_date));
    }
    echo "Days :".$days." Month :".date('F',$month);
    echo '<br>';
    $month = strtotime("+1 month", $month);
}

output will looks like below

输出将如下所示

Days :28 Month :February Days :31 Month :March Days :01 Month :April