PHP函数按年份检索两个日期之间的天数

时间:2018-11-27 14:16:10

标签: php date recursion multidimensional-array

我需要一个PHP函数来返回多维数组,以使每年的日期之间有每年的天数:

ES:

daysinyears('17-06-2012', '28-07-2018')

必须返回此数组:

Array ( [0] => Array ([year] => 2018, [days] => 208),
        [1] => Array ([year] => 2017, [days] => 364),
        [2] => Array ([year] => 2016, [days] => 365),
        [3] => Array ([year] => 2015, [days] => 364),
        [4] => Array ([year] => 2014, [days] => 364),
        [5] => Array ([year] => 2013, [days] => 365),
        [6] => Array ([year] => 2012, [days] => 197)
      )

这就是我所做的;

function daysinyears($d1, $d2) {
      // swap dates if wrong order
      if ($d1 > $d2) {
        $aux = $d1;
        $d1 = $d2;
        $d2 = $aux;
      }
      // if same year, just count days with date_diff
      if ($d1->format("Y") == $d2->format("Y")) {
        $days_year = array('year' => $d1->format("Y"), 
                             'days' => date_diff($d2, $d1)->format('%a'));
        $arr[] = $days_year;


      } else {
        $days_year = array('year' => $d2->format("Y"), 
                             'days' => date_diff($d2, date_create(date('d-m-Y',strtotime('01-01-'.$d2->format("Y")))))->format('%a'));
        $arr[] = $days_year;
        $year_prec = $d2->format("Y")-1;
        // Recursion
        $arr[] = daysinyears($d1, date_create(date('d-m-Y',strtotime('31-12-'.$year_prec))));
      }
      return $arr;
}

“有效”,但是问题在于递归在另一个这样的内部创建了一个数组:

[0] => Array (
    [year] => 2018
    [days] => 208
    )
[1] => Array (
    [0] => Array (
        [year] => 2017
        [days] => 364
        )
    [1] => Array (
        [0] => Array (
            [year] => 2016
            [days] => 365
            )
        [1] => Array (
            [0] => Array (
                [year] => 2015
                [days] => 364
                )
            [1] => Array (
                [0] => Array (
                    [year] => 2014
                    [days] => 364
                    )
                [1] => Array (
                    [0] => Array (
                        [year] => 2013
                        [days] => 364
                        )
                    [1] => Array (
                        [0] => Array (
                            [year] => 2012
                            [days] => 197
                            )
                        )
                    )
                )
            )
        )
    )

在不返回这些中文框的情况下,我无法理解如何使用递归。

3 个答案:

答案 0 :(得分:1)

与其将递归结果与df.drop_duplicates(subset = None, keep='first', inplace =True) 添加,不如将其与$arr[] =合并:

$arr = array_merge($arr, ...)

答案 1 :(得分:1)

我认为您的计算有误,因为2018年的总天数应该是178,而不是208。我还认为您可以使用更清晰的解决方案,例如:

function daysInYears($start, $end)
{
    $result = [];

    $start = new DateTime($start);
    $end = new DateTime($end);

    while ($start < $end) {

        $endOfYear = (clone $start)->modify('last day of december');

        if ($endOfYear > $end) {
            $endOfYear = $end;
        }

        $result[] = [
            'year' => $start->format('Y'),
            'days' => $endOfYear->diff($start)->days
        ];

        $start = $endOfYear->modify('+1 day');
    }

    return $result;
}

daysInYears('17-06-2012', '28-06-2018')的结果将是:

Array
(
    [0] => Array
        (
            [year] => 2012
            [days] => 197
        )

    [1] => Array
        (
            [year] => 2013
            [days] => 364
        )

    [2] => Array
        (
            [year] => 2014
            [days] => 364
        )

    [3] => Array
        (
            [year] => 2015
            [days] => 364
        )

    [4] => Array
        (
            [year] => 2016
            [days] => 365
        )

    [5] => Array
        (
            [year] => 2017
            [days] => 364
        )

    [6] => Array
        (
            [year] => 2018
            [days] => 178
        )

)

答案 2 :(得分:0)

您可以通过在参数上传递sl来解决问题。

function daysinyears($d1, $d2 , $sl) {
  // swap dates if wrong order
  if ($d1 > $d2) {
    $aux = $d1;
    $d1 = $d2;
    $d2 = $aux;
  }
  // if same year, just count days with date_diff
  if ($d1->format("Y") == $d2->format("Y")) {
    $days_year = array('year' => $d1->format("Y"), 
                         'days' => date_diff($d2, $d1)->format('%a'));
    $arr[$sl] = $days_year;


  } else {
    $days_year = array('year' => $d2->format("Y"), 
                         'days' => date_diff($d2, date_create(date('d-m-Y',strtotime('01-01-'.$d2->format("Y")))))->format('%a'));
    $arr[$sl] = $days_year;
    $year_prec = $d2->format("Y")-1;
    // Recursion
    $arr[$sl] = daysinyears($d1, date_create(date('d-m-Y',strtotime('31-12-'.$year_prec))),$sl++);
  }
  return $arr;


}

在调用函数的位置设置参数0