使用自定义规则计算两个日期之间的天数

时间:2015-01-19 06:11:36

标签: javascript php laravel

这是规则:

  • 每个月有30天(包括2月)。
  • 年限为360天。

以下是一个例子:

  • 初始日期:2/2/10
  • 结束日期:8/21/12

天数的计算将是:

  • 2/2/10至2/1/11 = 360
  • 2/2/11至2/1/12 = 360
  • 2/2/12至8/21/12 = 200

过去200天的计算方法如下:

  • 2/2/12至8/1/12 = 180天(6个月)
  • 8/2/12至8/21/12 = 20天

总计 920天

任何帮助都将受到赞赏并提前感谢。

2 个答案:

答案 0 :(得分:0)

感谢您的帮助,这就是我最终使用的内容:

$iDate = Carbon::createFromDate(2010, 2, 2);
$eDate = Carbon::createFromDate(2012, 8, 21);

$dateDiff = date_diff($iDate, $eDate);

$date = 0;
$date += $dateDiff->y * 360;
$date += $dateDiff->m * 30;
$date += $dateDiff->d + 1; // Need to add one more day

dd($date); // Outputs 920

这是使用包含Carbon包的Laravel。

答案 1 :(得分:0)

//2/2/10 to 2/1/11 = 360 days
$date_1 = "2/2/10";
$date_2 = "2/1/11";
$differenceFormat = "%a";
$diff = dateDifference($date_1 , $date_2 , $differenceFormat)   ;
echo "<br/>";
echo "$date_1 to $date_2 = $diff days";

//2/2/11 to 2/1/12 = 360 days
$date_1 = "2/2/11";
$date_2 = "2/1/12";
$differenceFormat = "%a";
$diff = dateDifference($date_1 , $date_2 , $differenceFormat)   ;
echo "<br/>";
echo "$date_1 to $date_2 = $diff days";

//2/2/12 to 8/21/12 = 200 days
$date_1 = "2/2/12";
$date_2 = "8/21/12";
$differenceFormat = "%a";
$diff = dateDifference($date_1 , $date_2 , $differenceFormat)   ;
echo "<br/>";
echo "$date_1 to $date_2 = $diff days";
//final
$date_1 = "2/2/10";
$date_2 = "8/21/12";
$differenceFormat = "%a";
$diff = dateDifference($date_1 , $date_2 , $differenceFormat)   ;
echo "<br/>";
echo "$date_1 to $date_2 = $diff days";
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);

}