php日期计算循环


php Date Calculation loop?

我有一个日期计算循环,但我不知道如何编码以实现正确的输出

这是我当前的代码

//This code is showing all dates from range of date
$year1 = '2011';
$month1 = '10';
$day1 = '17';
$day1 = $day1 + 1;
$year2 = '2012';
$month2 = '03';
$day2 = '17';
$start_date = "$year1-$month1-$day1";
echo "Start Date = $start_date ";
$end_date = "$year2-$month2-$day2";
echo "End Date = $end_date ";
$date = mktime(0,0,0,$month1,$day1,$year1); //Gets Unix timestamp START DATE
$date1 = mktime(0,0,0,$month2,$day2,$year2); //Gets Unix timestamp END DATE
$difference = $date1-$date; //Calcuates Difference
$daysago = floor($difference /60/60/24); //Calculates Days Old

$i = 0;
while ($i <= $daysago +1) {
if ($i != 0) { $date = $date + 86400; }
else { $date = $date - 86400; }
$today = date('Y-m-d',$date);
//echo "$today ";
$yy = date('Y',$date);
$mm = date('m',$date);
$dd = date('d',$date);
echo "$mm-$dd-$yy <br/>";
$i++;
}

以上代码显示

2011年10月18日2011年9月10日2011年10月20日2011年10月2011年10月22日2011年10月23日2011年10月24日2011年5月10日2011年10月26日2011年10月27日2011年10月28日2011年9月10日2011年10月30日2011年10月31日2011年1月11日2011年2月11日2011年3月11日2011年4月11日2011年5月11日。。等等2012年3月17日

我想获得特定月份的输出,如下面代码所示

11-17-2011
12-17-2011
01-17-2012
02-17-2012
03-17-2012

与其给用户一个日期的文本字段,不如给他们一些更简单的东西,比如三个下拉列表,每个下拉列表都有月份、日期和年份:

<select id='month' name='month'>
<option value='01'>Jan</option>
<option value='02'>Feb</option>
etc...
</select>
<select id='day' name='day'>
<option value='01'>01</option>
<option value='02'>02</option>
etc...

表单提交到的php:

$month = $_POST['month'];
$day = $_POST['day'];
$year = $_POST['year'];
$no_dates = $POST['no_dates']; //the number of dates
$i = 0;
while ($i < $no_dates){
date = mktime(0,0,0,$month+$i,$day,$year)
print date('Y-m-d', $date)."<br/>";
$i = $i + 1;
}

尝试这个

<?php
$dateMonthYearArr = array();
$fromDateTS = mktime(0,0,0,10,17,2011);
$toDateTS = mktime(0,0,0,3,17,2012);
for ($currentDateTS = $fromDateTS; $currentDateTS <= $toDateTS; $currentDateTS += (60 * 60 * 24)) {
    if (date('d',$currentDateTS)==17){
       $dateMonthYearArr[] = date('Y-m-d',$currentDateTS);
    }
}
echo  '<pre>';
print_r($dateMonthYearArr);
echo '</pre>';
?>

以下代码将产生您提到的输出:

$date1 = strtotime("2011-10-17");
$date2 = strtotime("2012-03-17");
$time = 0;
for($i = 0; ($time = strtotime("+$i month", $date1)) <= $date2; $i++) {
    echo date("m-d-Y'n", $time);
}

输出:

10-17-2011
11-17-2011
12-17-2011
01-17-2012
02-17-2012
03-17-2012