这是我的代码:
$d = new DateTime('2016-07-14');
$inc = new DateInterval('P1D');
$dateOptions = '';
//1=monday 2=tuesday 3=wednesday
$required = array(1,2,3,4);
$counter = $week = 0;
for ($i=0; $i<40; ++$i){
$d = $d->add($inc);
if (in_array($d->format('w'), $required)) {
if($counter % 4 == 0){
echo ($week + 1) . "<br />";
++$week;
}
$t = $d->format('l, F d, Y');
echo $t . "<br />";
++$counter;
}
}
这是输出:
1
Monday, July 18, 2016
Tuesday, July 19, 2016
Wednesday, July 20, 2016
Thursday, July 21, 2016
2
Monday, July 25, 2016
Tuesday, July 26, 2016
Wednesday, July 27, 2016
Thursday, July 28, 2016
我希望输出如下:
1
Thursday, July 14, 2016
2
Monday, July 18, 2016
Tuesday, July 19, 2016
Wednesday, July 20, 2016
Thursday, July 21, 2016
3
Monday, July 25, 2016
Tuesday, July 26, 2016
Wednesday, July 27, 2016
Thursday, July 28, 2016
需要对代码进行哪些更改才能允许星期四在第一周输出?
提前致谢!
答案 0 :(得分:1)
所以修改后的代码:
<?php
$d = new DateTime('2016-07-14');
$inc = new DateInterval('P1D');
$d = $d->sub($inc); // You need the start date from 14
$required = array(1,2,3,4);
$week = 0;
for ($i=0; $i<40; ++$i){
$d = $d->add($inc);
$weekNumber = $d->format('w');
if (in_array($weekNumber, $required)) {
if(!($weekNumber-1) % 4){ //Don't calculate the counter, but the week number
echo (++$week) . "<br />";
}
$t = $d->format('l, F d, Y');
echo $t . "<br />";
}
}
输出:
Thursday, July 14, 2016
1
Monday, July 18, 2016
Tuesday, July 19, 2016
Wednesday, July 20, 2016
Thursday, July 21, 2016
2
Monday, July 25, 2016
Tuesday, July 26, 2016
Wednesday, July 27, 2016
Thursday, July 28, 2016
3
Monday, August 01, 2016
Tuesday, August 02, 2016
Wednesday, August 03, 2016
Thursday, August 04, 2016
4
Monday, August 08, 2016
Tuesday, August 09, 2016
Wednesday, August 10, 2016
Thursday, August 11, 2016
5
Monday, August 15, 2016
Tuesday, August 16, 2016
Wednesday, August 17, 2016
Thursday, August 18, 2016
6
Monday, August 22, 2016
您的Eval
答案 1 :(得分:0)
使用以下代码,
for ($i=0; $i<40; ++$i){
if($i==0)
{
echo ($week + 1) . "<br />";
++$week;
$t = $d->format('l, F d, Y');
echo $t . "<br />";
}
$d = $d->add($inc);
if (in_array($d->format('w'), $required)) {
if($counter % 4 == 0){
echo ($week + 1) . "<br />";
++$week;
}
$t = $d->format('l, F d, Y');
echo $t . "<br />";
++$counter;
}
}
<强>输出:强>
1
Thursday, July 14, 2016
2
Monday, July 18, 2016
Tuesday, July 19, 2016
Wednesday, July 20, 2016
Thursday, July 21, 2016
3
Monday, July 25, 2016
Tuesday, July 26, 2016
Wednesday, July 27, 2016
Thursday, July 28, 2016
答案 2 :(得分:0)
这是我使用的最终代码:
$d = new DateTime('2016-08-29');
$inc = new DateInterval('P1D');
$d = $d->sub($inc);
$required = array(2,4); //1=monday 2=tuesday 3=wednesday 4=thursday 5=friday
$howmany = count($required);
$week = 0;
for ($i=0; $i<120; ++$i){
$d = $d->add($inc);
if (in_array($d->format('w'), $required)) {
if($counter % $howmany == 0){
echo ($week + 1) . "<br />";
++$week;
}
$t = $d->format('l, F d, Y');
echo $t . "<br />";
++$counter;
}
}
感谢您的帮助!