使用PHP计算两个日期之间的月数

时间:2017-04-24 09:21:38

标签: php

我的结束日期为2016-12-26,结束日期为2017-03-04

现在我想知道从给定时期开始每个月有多少天。上述期间日期(数组)的预期输出:

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

我怎样才能达到这种最干净的方式?我试过:

  1. 首先查看period_start,获取days = 26和
  2. 找出2016-12和2017-03之间月份的开始/结束日期,然后计算这里的天数(2月份分别为31天)
  3. 然后最终计算2017-03的4天。
  4. 但是有更清洁/更好的方法吗?

5 个答案:

答案 0 :(得分:2)

使用DateTime类可以轻松实现。创建对象,并在其上使用DateTime::diff(),然后使用days属性。

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

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

Live demo

答案 1 :(得分:1)

@Karem希望这个逻辑可以帮助你,这是适合你所有条件的工作案例,请在下面尝试一下:

<?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));
}

答案 2 :(得分:1)

这很长但很容易理解如何实现你的目标

<?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');
?>

现场演示:https://eval.in/781724

函数返回数组:https://eval.in/781741

答案 3 :(得分:0)

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

答案 4 :(得分:0)

我使用了Carbon(https://carbon.nesbot.com/docs/),但您可以在任何其他时间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;
}
相关问题