【问题标题】:get between two date all days for every Month in PHP在PHP中每个月的两个日期之间获取所有日期
【发布时间】:2017-05-31 10:47:51
【问题描述】:

我怎样才能在这样的数组中获取两天之间每个月的所有天数

$dateStart = "2016/12/14"; 
$dateFin =   "2017/04/21"

[2016/12/14 - 2016/12/31] => 17 days
[2017/01/01 - 2017/01/31] => 31 days
[2017/02/01 - 2017/02/28] => 28 days
[2017/03/01 - 2017/03/30] => 31 days
[2017/04/01 - 2017/04/21] => 21 days

【问题讨论】:

    标签: php arrays date days monthcalendar


    【解决方案1】:

    可以使用函数cal_days_in_month加DateTime类:

    <?php
    $dateStart = new DateTime("2016/12/14");
    $dateFin = new DateTime("2017/04/21");
    $firstDay = $dateStart->format('Y/m/d');
    $lastDay = $dateStart->format('Y/m/t');
    $totalMonths = $dateStart->diff($dateFin)->m + ($dateStart->diff($dateFin)->y*12);
    $result = [];
    for ($i = 0; $i <= $totalMonths; $i++)
    {
        if ($i != 0){
            $dateStart->modify('first day of next month');
            $firstDay = $dateStart->format('Y/m/d');
            $dateStart->modify('last day of month');
            $lastDay = $dateStart->format('Y/m/t');
        }
    
        $nextDate = explode('/', $firstDay);
    
        $totalDays = cal_days_in_month(CAL_GREGORIAN, $nextDate[1], $nextDate[2]);
        if ($i == 0){
            $totalDays -= $dateStart->format('d');
        } else if ($i == $totalMonths) {
            $totalDays = $dateFin->format('d');
        }
    
        $result["$firstDay - $lastDay"] = $totalDays;
    }
    
    var_dump($result);
    

    缺乏改进,但可以满足您的要求。

    常规改进,检查如下:

    $dateStart = new DateTime("2016/12/14");
    $dateFin = new DateTime("2017/04/21");
    $totalMonths = $dateStart->diff($dateFin)->m + ($dateStart->diff($dateFin)->y*12);
    $result = [];
    for ($i = 0; $i <= $totalMonths; $i++)
    {
        if ($i != 0){
            $obj = $dateStart->modify('first day of next month');
        }
    
        $firstDay = $dateStart->format('Y/m/d');
        if ($i == $totalMonths){
            $lastDay = $dateFin->format('Y/m/d');
        } else {
            $lastDay = $dateStart->format('Y/m/t');
        }
        $firstDayObj = strtotime($firstDay);
        $lastDayObj = strtotime($lastDay);
        $totalDays = (int) ceil(($lastDayObj - $firstDayObj) / 86400);
        $totalDays = ((int) $dateStart->format('d') == 1) ? $totalDays + 1 : $totalDays;
        $result["$firstDay - $lastDay"] = $totalDays;
    }
    
    var_dump($result);
    
    //array(5) { ["2016/12/14 - 2016/12/31"]=> int(17) ["2017/01/01 - 2017/01/31"]=> int(31) ["2017/02/01 - 2017/02/28"]=> int(28) ["2017/03/01 - 2017/03/31"]=> int(31) ["2017/04/01 - 2017/04/21"]=> int(21) }
    

    【讨论】:

    • 我会改进的,请稍等:)
    • 请检查我的改进
    • 我认为必须再增加一次循环,所以我必须在 totalMonths 中应用 +1 才能获得准确的结果。
    【解决方案2】:

    要获取两个日期的日期差异,您可以这样做:

    function daysDiff($d1, $d2) 
    {
        $x1 = days($d1);
        $x2 = days($d2);
    
        if ($x1 && $x2) {
            return abs($x1 - $x2);
        }
    }
    
    function days(DateTime $x) 
    {
        $y = $x->format('Y') - 1;
        $days = $y * 365;
        $z = (int)($y / 4);
        $days += $z;
        $z = (int)($y / 100);
        $days -= $z;
        $z = (int)($y / 400);
        $days += $z;
        $days += $x->format('z');
    
        return $days;
    }
    

    $d1 和 $d2 应该是 DateTime 类型,我从这里复制此代码:http://php.net/manual/es/function.date-diff.php#117691 并进行了小修改以使用类型提示而不是使用 get_class 验证变量类型

    【讨论】:

      【解决方案3】:

      Powerful Function to get two date difference

      查看演示here

      function dateDifference($date_1, $date_2, $differenceFormat = '%a')
      {
          $datetime1 = date_create($date_1);
          $datetime2 = date_create($date_2);
      
          $interval = date_diff($datetime1, $datetime2);
      
          return $interval->format($differenceFormat);
      }
      

      【讨论】:

        【解决方案4】:

        请尝试按照上述说明执行以下代码sn-p作为解决方案

        $start_date = new DateTime('2016/12/14');
        $start_date=$start_date->modify('+1 day');
        $end_date = new DateTime('2017/04/21');
        $end_date = $end_date->modify('+1 day');
        
        $interval = new DateInterval('P1D');
        $daterange = new DatePeriod($start_date, $interval ,$end_date);
        $month_days=array();
        foreach($daterange as $date)
        {
          $date_month=$date->format('m');
          $month_days[$date_month][]=$date->format('Y/m/d');
        }
        $result=array();
        foreach($month_days as $days)
        {
          $begin=reset($days);
          $end=end($days);
          $result[$begin.'-'.$end]=count($days);
        }
        echo '<pre>';
        print_r($result);
        

        【讨论】:

          猜你喜欢
          • 2015-05-20
          • 2013-08-09
          • 1970-01-01
          • 2012-07-04
          • 1970-01-01
          • 2017-06-12
          • 1970-01-01
          • 2014-06-11
          • 1970-01-01
          相关资源
          最近更新 更多