Php 循环年和月
如何使用给定的日期循环年份和月份? 下面是我当前的代码,无法运行Php 循环年和月,php,Php,如何使用给定的日期循环年份和月份? 下面是我当前的代码,无法运行 $Startdate = '2017-01'; $Enddate = '2018-06'; for($selectedDate = date("Y-m",$begin); $selectedDate <= date("Y-m",$end); $selectedDate++){ $resultY = date("Y",strtotime($selectedDate)); $resultM = date("m
$Startdate = '2017-01';
$Enddate = '2018-06';
for($selectedDate = date("Y-m",$begin); $selectedDate <= date("Y-m",$end); $selectedDate++){
$resultY = date("Y",strtotime($selectedDate));
$resultM = date("m",strtotime($selectedDate));
echo $resultY;
$echo resulthM;
}
如果我是你:)我将尝试使用
DateTime
类在你的$Startdate
和$Enddate
之间生成月份。看
如果我是你:)我将尝试使用DateTime
类在你的$Startdate
和$Enddate
之间生成月份。看
$startDate=new\DateTime('2017-01-01');
$endDate=new\DateTime('2018-06-01');
对于($selectedDate=$startDate;$selectedDate modify(“+1个月”)){
//格式$selectedDate;
$resultY=$selectedDate->format('Y');
$resultM=$selectedDate->format('m');
//印刷内容
echo$resultY;
echo“\t”;//打印选项卡
echo$resultM;
echo“\n”;//打印新行
}
$startDate=new\DateTime('2017-01-01');
$endDate=new\DateTime('2018-06-01');
对于($selectedDate=$startDate;$selectedDate modify(“+1个月”)){
//格式$selectedDate;
$resultY=$selectedDate->format('Y');
$resultM=$selectedDate->format('m');
//印刷内容
echo$resultY;
echo“\t”;//打印选项卡
echo$resultM;
echo“\n”;//打印新行
}
由于您熟悉strotime,因此可以将代码修改为以下内容以执行所需的结果
$begin = date("Y-m", strtotime("2017-01")); // Replace this with date to begin.
$end = date("Y-m"); // Replace this with date to end.
for($selectedDate = date("Y-m", strtotime($begin)); $selectedDate <= date("Y-m", strtotime($end)); $selectedDate = date("Y-m", strtotime($selectedDate . "+1 Month"))){
$resultY = date("Y", strtotime($selectedDate));
$resultM = date("m", strtotime($selectedDate));
echo $resultY;
echo $resultM;
}
$begin=日期(“Y-m”,标准时间(“2017-01”);//将此替换为开始日期。
$end=日期(“Y-m”);//将此替换为结束日期。
对于($selectedDate=date(“Y-m”,strotime($begin));$selectedDate,由于您熟悉strotime,因此可以将代码修改为以下内容以执行所需的结果
$begin = date("Y-m", strtotime("2017-01")); // Replace this with date to begin.
$end = date("Y-m"); // Replace this with date to end.
for($selectedDate = date("Y-m", strtotime($begin)); $selectedDate <= date("Y-m", strtotime($end)); $selectedDate = date("Y-m", strtotime($selectedDate . "+1 Month"))){
$resultY = date("Y", strtotime($selectedDate));
$resultM = date("m", strtotime($selectedDate));
echo $resultY;
echo $resultM;
}
$begin=date(“Y-m”,strotime(“2017-01”);//将其替换为开始日期。
$end=date(“Y-m”);//将其替换为截止日期。
对于($selectedDate=date(“Y-m”,strotime($begin));$selectedDate您可以使用DateTime::modify
并向前移动月份
$Startdate = '2017-01';
$Enddate = '2018-06';
$DateTime = new DateTime($Startdate.'-01');
echo $DateTime->format('Y')."\t".$DateTime->format('n')."\n";
do{
$DateTime->modify('+1 months');
echo $DateTime->format('Y')."\t".$DateTime->format('n')."\n";
if($DateTime->format('Y-m') == $Enddate) break;
}while(true);
只要确保Enddate
是有效的年/月,并且发生在Startdate
之后,或者在本例中,您将永远循环
可能还有其他方法可以避免这种情况
但是我没有看到答案中使用了DateTime::modify
,所以我想我会把它放在一起
输出:
2017 1
2017 2
2017 3
2017 4
2017 5
2017 6
2017 7
2017 8
2017 9
2017 10
2017 11
2017 12
2018 1
2018 2
2018 3
2018 4
2018 5
2018 6
当我想为月份之类的东西做一个选择/选项时,我通常使用这种方法。但是我使用这种格式stru-pad($DateTime->format('n'),2',,stru-pad_-LEFT)。-“-”。$DateTime->format('F')
或'2-二月'
,注意左边的空格…)…这样他们就排得很整齐
无论如何,干杯!您可以使用DateTime::modify
并向前走几个月
$Startdate = '2017-01';
$Enddate = '2018-06';
$DateTime = new DateTime($Startdate.'-01');
echo $DateTime->format('Y')."\t".$DateTime->format('n')."\n";
do{
$DateTime->modify('+1 months');
echo $DateTime->format('Y')."\t".$DateTime->format('n')."\n";
if($DateTime->format('Y-m') == $Enddate) break;
}while(true);
只要确保Enddate
是有效的年/月,并且发生在Startdate
之后,或者在本例中,您将永远循环
可能还有其他方法可以避免这种情况
但是我没有看到答案中使用了DateTime::modify
,所以我想我会把它放在一起
输出:
2017 1
2017 2
2017 3
2017 4
2017 5
2017 6
2017 7
2017 8
2017 9
2017 10
2017 11
2017 12
2018 1
2018 2
2018 3
2018 4
2018 5
2018 6
当我想为月份之类的东西做一个选择/选项时,我通常使用这种方法。但是我使用这种格式str\u-pad($DateTime->format('n'),2',,str\u-pad\u-LEFT)。'-'.$DateTime->format('F')
或'2-二月'
,注意左边的空格…:)。。。这样他们就排成了整齐的队伍
无论如何,干杯 如果$selectedDate
是2017-1
,那么您对$selectedDate++
的期望是什么?$echo resulthM代码>键入如果$selectedDate
是2017-1
,那么您对$selectedDate++
的期望是什么?$echo resulthM代码>打字感谢这看起来不错,但是这只给了我2017-01到2018-05的时间。感谢我刚刚添加的$period=new DatePeriod($start,$interval,$end->modify(“+1个月”)代码>有效:)很高兴它能帮到你:)谢谢这看起来不错,但这只给我2017-01到2018-05谢谢我刚刚添加的$period=new DatePeriod($start,$interval,$end->modify(“+1个月”)代码>而且它能工作:)很高兴它能帮助你:)哦,是的,它在那里。我也要试试这个。因此,我认为$selectedDate++在中处理日期时不起作用loop@JydonMah事实上,我相信这是事实,你甚至增加$selectedDate$selectedDate++表示将一个添加到$selectedDate。。。但是加一个什么?我相信这就是问题所在。用数学术语来说,你有数字,但没有单位。哦,是的,在那里。我也要试试这个。因此,我认为$selectedDate++在中处理日期时不起作用loop@JydonMah事实上,我相信这是事实,你甚至增加$selectedDate$selectedDate++表示将一个添加到$selectedDate。。。但是加一个什么?我相信这就是问题所在。用数学术语来说,你有数字,但没有单位。