根据360天获得两个日期之间的差异

时间:2014-04-02 16:10:23

标签: php date diff

如何根据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;

2 个答案:

答案 0 :(得分:2)

最好的解决方案:

<?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 article

还有许多其他功能选项,但今天,OOP方式更好。

更新: 360天的事情

年:

$years = ($diff->days - ($diff->days % 360)) / 360; //+some remaining days if any

月:根据维基页面和美国/ NASD方法(30US / 360)

$months = ($diff->days - ($diff->days % 30)) / 30; //+some remaining days if any

答案 1 :(得分:0)

我也是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;