如何使用 PHP 循环列出 yyyy_mm 格式的日期?

发布于 2024-12-09 09:00:41 字数 722 浏览 1 评论 0原文

在 PHP 中使用循环按以下方式列出日期的最简洁方法是什么?

2011_10
2011_09
2011_08
2011_07
2011_06
...
2010_03
2009_02
2009_01
2009_12
2009_11

这里的关键要素:

  • 应尽可能简单 - 我更喜欢一个 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";
    }

What's the cleanest way to use a loop in PHP to list dates in the following way?

2011_10
2011_09
2011_08
2011_07
2011_06
...
2010_03
2009_02
2009_01
2009_12
2009_11

The key elements here:

  • Should be as simple as possible - I would prefer one for loop instead of two.
  • Should list this month's date as the first date, and should stop at a fixed point (2009-11)
  • Should not break in the future (eg: subtracting 30 days worth of seconds will probably work but will eventually break as there are not an exact amount of seconds on each month)

Had to make a few tweaks to the solution:

    // 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";
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

断肠人 2024-12-16 09:00:41

也许这个小代码可以做到这一点? :
更复杂的情况。

    <?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)));
        }

 ?>

功劳归于: http://www.if-not-true-then-false.com/2009/php-loop-through-dates-from-date-to-date-with-strtotime-function /

Maybe this little code does the thing? :
more complicated situations.

    <?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)));
        }

 ?>

The credit goes to: http://www.if-not-true-then-false.com/2009/php-loop-through-dates-from-date-to-date-with-strtotime-function/

季末如歌 2024-12-16 09:00:41

这就是我猜你要求的,因为它真的没有意义......

$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

This is what im guessing your asking for cause it doesnt really make sense......

$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
浅笑轻吟梦一曲 2024-12-16 09:00:41

PHP 5.3 对 PHP 中的日期/时间处理引入了一些重大改进。例如,下面使用的第一天DateIntervalDatePeriod

$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 introduces some great improvements to date/time processing in PHP. For example, the first day of, DateInterval and DatePeriod being used below.

$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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文