最初我试图创建一个函数来显示某一特定日期落入特定日期的次数。例如,星期六在某年某年的1月1日落幕多少次。计算有多少天落入特定日期范围内的年份
<?php
$firstDate = '01/01/2000';
$endDate = '01/01/2012';
$newYearDate= '01/01';
# convert above string to time
$time1 = strtotime($firstDate);
$time2 = strtotime($endDate);
$newYearTime = strtotime($newYearDate);
for($i=$time1; $i<=$time2; $i++){
$saturday = 0;
$chk = date('D', $newYearTime); #date conversion
if($chk == 'Sat' && $chk == $newYearTime){
$saturday++;
}
}
echo $saturday;
?>
但它需要永久循环 –
我会这样做的方式是1.找到第一个星期你想从$ firstDate开始的第二天2.查找从星期几(不是$ firstDate)到$ endDate的总天数3. $ total_number_of_days%7,应该是。 – kennypu
@kennypu这看起来不对。如果您将一天添加到结束日期,您的过程将为结果添加1,但结果通常不应更改。 – Barmar