我在日期计算方面遇到了一些麻烦。我会尽可能多地解释它;
假设您有一个广告牌,并且该媒体具有有效的日期范围。例如,有效期限的开始日期是每年的 12 月 1 日,有效期限的结束日期是每年的 3 月 1 日。
假设您将此广告牌租给某人一段时间。示例日期范围为 2017 年 2 月 1 日至 2020 年 12 月 10 日。在此期间,我如何计算计费日期计数?试图用php来做,但卡住了。任何人都可以在没有太多“if”子句的情况下帮助我做到这一点?
编辑:
$activeTermStart = "01/12"; // 'd/m'
$activeTermEnd = "01/03"; // 'd/m'
$daterangeStart = new \DateTime('2017-02-01');
$daterangeEnd = new \DateTime('2020-12-10');
//check if active term start&end have year diff
$hasYearDiffBetweenTermStartAndEnd = false;
$startArray = explode('/',$activeTermStart);
$endArray = explode('/',$activeTermEnd);
if ($startArray[1]>$endArray[1]) {
$hasYearDiffBetweenTermStartAndEnd = true;
}
if ($startArray[1]==$endArray[1] && $startArray[0]>=$endArray[0]) {
$hasYearDiffBetweenTermStartAndEnd = true;
}
$startTermWithYear = \DateTime::createFromFormat('d/m/Y',$activeTermStart.'/'.date('Y'));
$startTermWithFirstYear = \DateTime::createFromFormat('d/m/Y',$activeTermStart.'/'.$daterangeStart->format('Y'));
if ($hasYearDiffBetweenTermStartAndEnd) {
$endTermWithYear = \DateTime::createFromFormat('d/m/Y',$activeTermEnd.'/'.(date('Y')+1));
$endTermWithFirstYear = \DateTime::createFromFormat('d/m/Y',$activeTermEnd.'/'.((int)$daterangeStart->format('Y')+1);
} else {
$endTermWithYear = \DateTime::createFromFormat('d/m/Y',$activeTermEnd.'/'.date('Y'));
$endTermWithFirstYear = \DateTime::createFromFormat('d/m/Y',$activeTermEnd.'/'.$daterangeStart->format('Y'));
}
$maximumBillableDaysPerYear = ($endTermWithYear->getTimestamp() - $startTermWithYear->getTimestamp())/60/60/24;
$totalYearsToCheck = ((int)$daterangeEnd->format('Y') - (int)$daterangeStart->format('Y'))+1;
$maximumBillableDaysTotal = $totalYearsToCheck * $maximumBillableDaysPerYear;
// from that moment on, i want to see how many days are not in the first year and
// how many days are not in the last year. Remove them from the maximumBillableDaysTotal to
// see how many days that billboard is billable for.
// stucked here...