我正在尝试根据放入表单中的值显示连续几周,我有一些我编写的代码可行,但我遇到的问题是我似乎无法让“周#”正常工作我希望有人能让我知道我哪里出错了
<?php
$teams = $_REQUEST['teams'];
$weeks = $_REQUEST['weeks'];
$rounds = $_REQUEST['rounds'];
$start = new DateTime($_REQUEST['start']);
$end = new DateTime($_REQUEST['end']);
$end = $end->modify( '+1 week' );
$a = array($weeks, $rounds);
echo "Length of Session = " . array_product($a) . " weeks"."<br>" . "\n";
for($i = 0; $i<=array_product($a); $i++){
}
$interval = new DateInterval('P1W');
$daterange = new DatePeriod($start, $interval ,$end);
foreach($daterange as $date){
echo "week $i - " . $date->format("m/d/Y") . "<br>";
}
?>
此代码显示以下内容:
Length of Session = 20 weeks
week 21 - 11/14/2017
week 21 - 11/21/2017
week 21 - 11/28/2017
week 21 - 12/05/2017
week 21 - 12/12/2017
week 21 - 12/19/2017
week 21 - 12/26/2017
week 21 - 01/02/2018
week 21 - 01/09/2018
week 21 - 01/16/2018
week 21 - 01/23/2018
week 21 - 01/30/2018
week 21 - 02/06/2018
week 21 - 02/13/2018
week 21 - 02/20/2018
week 21 - 02/27/2018
week 21 - 03/06/2018
week 21 - 03/13/2018
week 21 - 03/20/2018
week 21 - 03/27/2018
答案 0 :(得分:1)
这是您更正后的代码。您不需要for
循环。只需在foreach循环之前初始化$i
变量,然后在foreach
循环内增加它:
$teams = $_REQUEST['teams'];
$weeks = $_REQUEST['weeks'];
$rounds = $_REQUEST['rounds'];
$start = new DateTime($_REQUEST['start']);
$end = new DateTime($_REQUEST['end']);
$end = $end->modify( '+1 week' );
$a = array($weeks, $rounds);
echo "Length of Session = " . array_product($a) . " weeks"."<br>" . "\n";
// You don't need this:
// for($i = 0; $i<=array_product($a); $i++){
// }
$interval = new DateInterval('P1W');
$daterange = new DatePeriod($start, $interval ,$end);
$i = 1;
foreach($daterange as $date){
echo "week $i - " . $date->format("m/d/Y") . "<br>";
$i++;
}