如何计算 360 天的两个日期之间的差异?
360 天:http://en.wikipedia.org/wiki/360-day_calendar
我想得到天、年和月的差异。
例如:
$fechaDT1 = new DateTime($fechauno);
$fechaDT2 = new DateTime($fechados);
//$initialdays = 30 - (float)$fechaDT1->format('d');
$years = $fechaDT1->format('Y') - $fechaDT2->format('Y');
$months = $fechaDT1->format('m') - $fechaDT2->format('m');
$days = (-$fechaDT1->format('d') + $fechaDT2->format('d'));
$totalDay = $months*30 +$days;
解决方案:
$startDate = new DateTime($startDate);
$endDate = new DateTime($endDate);
$initialDays = 30 - $startDate->format('d');
$year = ($endDate->format('Y') - $startDate->format('Y')) * 360;
$meses = ($endDate->format('m') - $startDate->format('m')) * 30;
$dias = ($endDate->format('d') - $startDate->format('d'));
$totalDays = $year+$meses+$dias;
$years = number_format($totalDias/360);
$diff = $years - ($endDate->diff($startDate)->y);
$daysR = $totalDays - (($years-$diff)*360);
$result = array("days" => $daysR, "years" => ($years-$diff), "initial days" => $initialDays);
return $result;
最好的解决方案:
<?php
$date1 = new DateTime('2013-03-24');
$date2 = new DateTime('2014-03-24');
$diff = $date1->diff($date2);
// Do whatever you want
echo $diff->days;
var_dump($diff);
参考 Nettuts 文章
还有许多其他功能选项,但今天,OOP 方式更好。
更新:360天的事情
年份:
$years = ($diff->days - ($diff->days % 360)) / 360; //+some remaining days if any
月份:根据 wiki 页面和以下 US/NASD 方法 (30US/360):
$months = ($diff->days - ($diff->days % 30)) / 30; //+some remaining days if any
我也是 Python 新手,但我认为这会起作用:
import datetime as dt
import calendar
根据30/360日历计算天数差异 格式 date1 : 日期格式 (2020-01-01) date2 : 日期格式 (2020-01-01)
def days_360 (date1, date2):
days_diff = (date2.year - date1.year) * 360;
days_diff += (date2.month - date1.month) * 30;
days_diff += (date2.day - date1.day);
return days_diff;