如何使用循环使用PHP以yyyy_mm格式列出日期?
在PHP中使用循环以以下方式列出日期最干净的方法是什么如何使用循环使用PHP以yyyy_mm格式列出日期?,php,date,loops,time,for-loop,Php,Date,Loops,Time,For Loop,在PHP中使用循环以以下方式列出日期最干净的方法是什么 2011_10 2011_09 2011_08 2011_07 2011_06 ... 2010_03 2009_02 2009_01 2009_12 2009_11 这里的关键要素是: 应该尽可能简单-我更喜欢一个for循环而不是两个for循环 应将本月的日期列为第一个日期,并应在固定点停止(2009-11) 不应在将来中断(例如:减去30天的秒数可能有效,但最终会中断,因为每个月没有确切的秒数) 必须对解决方案进行一些调整:
2011_10
2011_09
2011_08
2011_07
2011_06
...
2010_03
2009_02
2009_01
2009_12
2009_11
这里的关键要素是:
- 应该尽可能简单-我更喜欢一个for循环而不是两个for循环
- 应将本月的日期列为第一个日期,并应在固定点停止(2009-11)
- 不应在将来中断(例如:减去30天的秒数可能有效,但最终会中断,因为每个月没有确切的秒数)
// Set timezone
date_default_timezone_set('UTC');
// Start date
$date = date('Y').'-'.date('m').'-01';
// End date
$end_date = '2009-1-1';
while (strtotime($date) >= strtotime($end_date))
{
$date = date ("Y-m-d", strtotime("-1 month", strtotime($date)));
echo substr($date,0,7);
echo "\n";
}
也许这个小代码能起作用 更复杂的情况
<?php
// Set timezone
date_default_timezone_set('UTC');
// Start date
$date = '2009-12-06';
// End date
$end_date = '2020-12-31';
while (strtotime($date) <= strtotime($end_date)) {
echo "$date\n";
$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
}
?>
功劳归于:也许这个小代码可以做到这一点: 更复杂的情况
<?php
// Set timezone
date_default_timezone_set('UTC');
// Start date
$date = '2009-12-06';
// End date
$end_date = '2020-12-31';
while (strtotime($date) <= strtotime($end_date)) {
echo "$date\n";
$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
}
?>
功劳归于:我猜这就是你的要求,因为它没有真正的意义
$startmonth = date("m");
$endmonth = 7;
$startyear = date("Y");
$endyear = 2012;
//First for loop to loop threw years
for($i=$startyear; $i<=$endyear; $i++, $startmonth=0) {
//Second for loop to loop threw months
for($o=$startmonth; $o<=12; $o++) {
//If statement to check and throw stop when at limits
if($i == $endyear && $o <= $endmonth)
echo $i."_".$o."<br/>";
else
break;
}
}
Will output:
2012_0
2012_1
2012_2
2012_3
2012_4
2012_5
2012_6
2012_7
$startmonth=日期(“m”);
$endmonth=7;
$startyear=日期(“Y”);
$endyear=2012年;
//第一个循环到第二年
对于($i=$startyear;$i这就是我猜你的要求,因为它没有真正意义
$startmonth = date("m");
$endmonth = 7;
$startyear = date("Y");
$endyear = 2012;
//First for loop to loop threw years
for($i=$startyear; $i<=$endyear; $i++, $startmonth=0) {
//Second for loop to loop threw months
for($o=$startmonth; $o<=12; $o++) {
//If statement to check and throw stop when at limits
if($i == $endyear && $o <= $endmonth)
echo $i."_".$o."<br/>";
else
break;
}
}
Will output:
2012_0
2012_1
2012_2
2012_3
2012_4
2012_5
2012_6
2012_7
$startmonth=日期(“m”);
$endmonth=7;
$startyear=日期(“Y”);
$endyear=2012年;
//第一个循环到第二年
对于($i=$startyear;$iPHP5.3,它对PHP中的日期/时间处理进行了一些重大改进。例如,下面使用的第一天
,日期间隔
和日期周期
$start = new DateTime('first day of this month');
$end = new DateTime('2009-11-01');
$interval = new DateInterval('P1M');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
echo $date->format('Y_m') . PHP_EOL;
}
PHP 5.3对PHP中的日期/时间处理进行了一些重大改进。例如,下面使用的第一天
,日期间隔
和日期周期
$start = new DateTime('first day of this month');
$end = new DateTime('2009-11-01');
$interval = new DateInterval('P1M');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
echo $date->format('Y_m') . PHP_EOL;
}
标题和正文不匹配。您想要哪种格式?标题和正文不匹配。您想要哪种格式?