如何在插入周数的情况下使输出看起来像这样?
Week 1
Monday, January 18, 2016
Wednesday, January 20, 2016
Friday, January 22, 2016
Week 2
Monday, January 25, 2016
Wednesday, January 27, 2016
Friday, January 29, 2016
Week 3
Monday, February 01, 2016
Wednesday, February 03, 2016
Friday, February 05, 2016
这是我的代码:
$d = new DateTime('2016-01-17');
$inc = new DateInterval('P1D');
$dateOptions = '';
$required = array(1,3,5);
for ($i=0; $i<28; ++$i) {
$d = $d->add($inc);
if (in_array($d->format('w'), $required)) {
$t = $d->format('l, F d, Y');
echo "$t<br \>";
}
}
提前致谢。
答案 0 :(得分:0)
$d = new DateTime('2016-01-17');
$inc = new DateInterval('P1D');
$dateOptions = '';
$required = array(1,3,5);
$j = 1;
for ($i = 0; $i <28; ++$i) {
$d = $d->add($inc);
if($d->format('w') == 1){
echo 'Week '.$j.PHP_EOL;
$j++;
}
if (in_array($d->format('w'), $required)) {
$t = $d->format('l, F d, Y');
echo $t.PHP_EOL;
}
}
答案 1 :(得分:0)
您可以这样做:
$d = new DateTime('2016-01-17');
$inc = new DateInterval('P1D');
$dateOptions = '';
$required = array(1,3,5);
$counter = $week = 0;
for ($i=0; $i<28; ++$i){
$d = $d->add($inc);
if (in_array($d->format('w'), $required)) {
if($counter % 3 == 0){
echo "Week " . ($week + 1) . "<br />";
++$week;
}
$t = $d->format('l, F d, Y');
echo $t . "<br />";
++$counter;
}
}
输出:
Week 1
Monday, January 18, 2016
Wednesday, January 20, 2016
Friday, January 22, 2016
Week 2
Monday, January 25, 2016
Wednesday, January 27, 2016
Friday, January 29, 2016
Week 3
Monday, February 01, 2016
Wednesday, February 03, 2016
Friday, February 05, 2016
Week 4
Monday, February 08, 2016
Wednesday, February 10, 2016
Friday, February 12, 2016
$counter
将跟踪正在打印的日期数量,$week
将在每三个日期之后打印周数。
答案 2 :(得分:0)
这应该做你想要的......
$d = new DateTime('2016-01-17');
$inc = new DateInterval('P1D');
$dateOptions = '';
$required = array(1,3,5);
$week = null;
$week_no = 0;
for ($i=0; $i<28; ++$i) {
$d = $d->add($inc);
if (in_array($d->format('w'), $required)) {
if($week!=$d->format('W')){
$week_no++;
$week = $d->format('W');
echo "Week $week_no<br \>";
}
$t = $d->format('l, F d, Y');
echo "$t<br \>";
}
}
$week
变量跟踪日期周数(来自PHP date()
函数)。
$week_no
会跟踪从1开始的增量周数。
if语句检查当前周数是否与前几周不同($week
)。如果它不同,则为$week_no
添加1,a与之前的
答案 3 :(得分:0)
检查http://php.net/manual/en/function.date.php
W =&gt; ISO-8601周的一周,星期一开始的星期(在PHP 4.1.0中添加)
如果在打印日期之前需要添加此内容
if ($d->format('w') == 1){
echo "Week " . $d->format('W') . "<br \>";
}
这样的事情可以解决问题
$d = new DateTime('2016-01-17');
$inc = new DateInterval('P1D');
$dateOptions = '';
$required = array(1,3,5);
for ($i=0; $i<28; ++$i) {
$d = $d->add($inc);
if ($d->format('w') == 1){
echo "Week " . $d->format('W') . "<br \>";
}
if (in_array($d->format('w'), $required)) {
$t = $d->format('l, F d, Y');
echo "$t<br \>";
}
}
输出:
Week 01
Monday, January 18, 2016
Wednesday, January 20, 2016
Friday, January 22, 2016
Week 02
Monday, January 25, 2016
Wednesday, January 27, 2016
Friday, January 29, 2016
Week 03
Monday, February 01, 2016
Wednesday, February 03, 2016
Friday, February 05, 2016
Week 04
Monday, February 08, 2016
Wednesday, February 10, 2016
Friday, February 12, 2016
答案 4 :(得分:0)
尝试使用此代码。
$d = new DateTime('2016-01-17');
$inc = new DateInterval('P1D');
$dateOptions = '';
$required = array(1,3,5);
$counter = $week = 0;
for ($i=0; $i<21; ++$i){
$d = $d->add($inc);
if (in_array($d->format('w'), $required)) {
if($counter % 3 == 0){
$week_num = $week + 1;
echo "Week $week_num<br />";
++$week;
}
$t = $d->format('l, F d, Y');
echo "$t<br />";
++$counter;
}
}
输出
Week 1
Monday, January 18, 2016
Wednesday, January 20, 2016
Friday, January 22, 2016
Week 2
Monday, January 25, 2016
Wednesday, January 27, 2016
Friday, January 29, 2016
Week 3
Monday, February 01, 2016
Wednesday, February 03, 2016
Friday, February 05, 2016