2

我创建了一个循环,它将以格式化的方式显示日期 2004 到 2014。但问题是,它显示的是 204 而不是 2004 并一直持续到 209 .. 那么,如何以格式化的方式显示那些年份,如 2004、2005、2006 等。
这是我创建的代码,告诉我在哪里使固定:

<?php
    $yr = 4;
    while ($yr <= 14) {
        $x = 1;
        while ($x <= 31) {
            echo "$x Jan 20$yr<br>";
            $x++;
        }
        $x = 1;
        while ($x <= 31) {
            echo "$x Feb 20$yr<br>";
            $x++;
        }
        $x = 1;
        while ($x <= 31) {
            echo "$x Mar 20$yr<br>";
            $x++;
        }
        $x = 1;
        while ($x <= 31) {
            echo "$x Apr 20$yr<br>";
            $x++;
        }
        $x = 1;
        while ($x <= 31) {
            echo "$x May 20$yr<br>";
            $x++;
        }
        $x = 1;
        while ($x <= 31) {
            echo "$x Jun 20$yr<br>";
            $x++;
        }
        $x = 1;
        while ($x <= 31) {
            echo "$x Jul 20$yr<br>";
            $x++;
        }
        $x = 1;
        while ($x <= 31) {
            echo "$x Aug 20$yr<br>";
            $x++;
        }
        $x = 1;
        while ($x <= 31) {
            echo "$x Sep 20$yr<br>";
            $x++;
        }
        $x = 1;
        while ($x <= 31) {
            echo "$x Oct 20$yr<br>";
            $x++;
        }
        $x = 1;
        while ($x <= 31) {
            echo "$x Nov 20$yr<br>";
            $x++;
        }
        $x = 1;
        while ($x <= 31) {
            echo "$x Dec 20$yr<br>";
            $x++;
        }
        $yr++;
    }
?>
4

10 回答 10

6

您只需要一个循环

$start = 2004;
$end = 2014;

$dateTime = new DateTime();
$dateTime->setDate($start, 1, 1);

echo "<pre>";
while ( $dateTime->format("Y") <= $end ) {
    echo $dateTime->format("d M Y"), PHP_EOL;
    $dateTime->modify("+1 day");
}
于 2012-11-05T14:16:24.140 回答
5

当你可以做这样的事情时,为什么要经历如此漫长而奇怪的过程?

<?php
    $yearStart = 2004;
    $yearEnd = 2012;
    $unixTime = strtotime($yearStart . "-01-01 00:00:00");
    $endUnixTime = strtotime($yearEnd . "-12-31 23:59:59");
    while ($unixTime < $endUnixTime) {
        echo date("d M Y", $unixTime) . PHP_EOL;
        $unixTime = strtotime("+1 day", $unixTime);
    }
?>

输出:

01 Jan 2004
02 Jan 2004
03 Jan 2004
...
29 Dec 2012
30 Dec 2012
31 Dec 2012

这还具有不显示“2008 年 2 月 31 日”等的额外好处,因为该日期甚至不存在。

代码的键盘示例(警告:长输出!)

于 2012-11-05T14:15:41.473 回答
3

我不确定您为什么要通过这么多循环来执行此操作,使用它而不是 $yr ,您将打印出正确的年份:

 str_pad($yr, 2, '0', STR_PAD_LEFT);

最好的

于 2012-11-05T14:18:31.663 回答
2

最简单的解决方法是设置 $yr = 2004 并在 $yr < 2014 时循环。您没有用前导零填充数字,因此是 204、205 等。

于 2012-11-05T14:13:16.153 回答
2

您也可以使用(相当)不同的结构来做到这一点:

<?php
    function displayDate($yr, $yrMax) {
        if ($yr > $yrMax) {
            return true;
        }
        else {
            displayMonth($yr);
            $yr++;
            return displayDate($yr, $yrMax);
        }
    }
    function displayMonth($yr, $month = 1) {
        if ($month > 12) {
            return true;
        }
        else {
            displayDay($yr, $month);
            return displayMonth($yr, $month+1);
        }
    }
    function displayDay($yr, $month, $day = 1, $dayMax = 31) {
            if ($day > $dayMax) {
                return true;
            } else {
                $displayMonth = getMonth($month);
                echo "$day $displayMonth $yr<br>";
                $day++;
                return displayDay($yr, $month, $day, $dayMax);
            }
    }
    function getMonth($month) {
        switch($month){
            case 1:
                return 'Jan';
            case 2:
                return 'Feb';
            case 3:
                return 'Mar';
            case 4:
                return 'Apr';
            case 5:
                return 'May';
            case 6:
                return 'Jun';
            case 7:
                return 'Jul';
            case 8:
                return 'Aug';
            case 9:
                return 'Sep';
            case 10:
                return 'Oct';
            case 11:
                return 'Nov';
            case 12:
                return 'Dec';
        }
    }

            //Here we call the structure build above.
    if (displayDate(2004, 2014)) {
        echo 'Done';
    }
?>
于 2012-11-05T14:41:40.590 回答
1

根据你的代码,你可以试试这个。虽然它不是标准方式:

<?php
$yar = 4;
while ($yar <= 9) {
    $ax = 1;
    while ($ax <= 31) {
        echo "$ax Jan 200$yar <br>";
        $ax++;
    }
    $ax = 1;
    while ($ax <= 31) {
        echo "$ax Feb 200$yar <br>";
        $ax++;
    }
    $ax = 1;
    while ($ax <= 31) {
        echo "$ax Mar 200$yar <br>";
        $ax++;
    }
    $ax = 1;
    while ($ax <= 31) {
        echo "$ax Apr 200$yar <br>";
        $ax++;
    }
    $ax = 1;
    while ($ax <= 31) {
        echo "$ax May 200$yar <br>";
        $ax++;
    }
    $ax = 1;
    while ($ax <= 31) {
        echo "$ax Jun 200$yar <br>";
        $ax++;
    }
    $ax = 1;
    while ($ax <= 31) {
        echo "$ax Jul 200$yar <br>";
        $ax++;
    }
    $ax = 1;
    while ($ax <= 31) {
        echo "$ax Aug 200$yar <br>";
        $ax++;
    }
    $ax = 1;
    while ($ax <= 31) {
        echo "$ax Sep 200$yar <br>";
        $ax++;
    }
    $ax = 1;
    while ($ax <= 31) {
        echo "$ax Oct 200$yar <br>";
        $ax++;
    }
    $ax = 1;
    while ($ax <= 31) {
        echo "$ax Nov 200$yar <br>";
        $ax++;
    }
    $ax = 1;
    while ($ax <= 31) {
        echo "$ax Dec 200$yar <br>";
        $ax++;
    }
    $yar++;
}
$yr = 10;
while ($yr <= 14) {
    $x = 1;
    while ($x <= 31) {
        echo "$x Jan 20$yr <br>";
        $x++;
    }
    $x = 1;
    while ($x <= 31) {
        echo "$x Feb 20$yr <br>";
        $x++;
    }
    $x = 1;
    while ($x <= 31) {
        echo "$x Mar 20$yr <br>";
        $x++;
    }
    $x = 1;
    while ($x <= 31) {
        echo "$x Apr 20$yr <br>";
        $x++;
    }
    $x = 1;
    while ($x <= 31) {
        echo "$x May 20$yr <br>";
        $x++;
    }
    $x = 1;
    while ($x <= 31) {
        echo "$x Jun 20$yr <br>";
        $x++;
    }
    $x = 1;
    while ($x <= 31) {
        echo "$x Jul 20$yr <br>";
        $x++;
    }
    $x = 1;
    while ($x <= 31) {
        echo "$x Aug 20$yr <br>";
        $x++;
    }
    $x = 1;
    while ($x <= 31) {
        echo "$x Sep 20$yr <br>";
        $x++;
    }
    $x = 1;
    while ($x <= 31) {
        echo "$x Oct 20$yr <br>";
        $x++;
    }
    $x = 1;
    while ($x <= 31) {
        echo "$x Nov 20$yr <br>";
        $x++;
    }
    $x = 1;
    while ($x <= 31) {
        echo "$x Dec 20$yr <br>";
        $x++;
    }
    $yr++;
}
?>
于 2012-11-06T04:14:08.730 回答
0

这不是标准的,但您可以在该 while 循环中添加一个 if 循环,如下所示:

while($x <= 31) 
{
if ($yr>=10)
    {
       echo "$x Oct 20$yr<br>";
    }

else
    {
       echo "$x Oct 200$yr<br>";
    }

$x++;
}

虽然你想做这样的事情让我失望

于 2012-11-05T14:14:35.687 回答
0

这是因为你是这样设置$yr的:

$yr = 4;

试试这个:

$yr = sprintf('%02d', $yr);
echo "$x Jan 20$yr<br>";
于 2012-11-05T14:14:56.390 回答
0

您需要使用该str_pad功能(手册)。在你的情况下,它是这样的:

<?php 

    $yr = 4;
    while ($yr <= 14) {
        $x = 1;
        while ($x <= 31) {
            echo "$x Jan 20".str_pad($yr, 2, "0",STR_PAD_LEFT)."<br>";
            $x++;
        }
        $yr++;
    }
?>
于 2012-11-05T14:23:45.577 回答
0

使用str_pad

echo $x.' Jan 20'.str_pad($yr, 2, '0', STR_PAD_LEFT).'<br>';  

更适合使用 变量$x的函数cal_days_in_month

<?php

    $yr = 4;
    while ($yr <= 14) {

        $year = '20'.str_pad($yr, 2, '0', STR_PAD_LEFT);

        for($month = 1; $month <= 12; $month++) {

            //number of days this month
            $daysCount = cal_days_in_month(CAL_GREGORIAN, $month, $year); 
            //catches the month spelled 
            $timestamp = mktime(0, 0, 0, $month, 1, $year);
            $monthText = date('M', $timestamp);

            for($day = 1; $day <= $daysCount; $day++) {
                echo $day.' '.$monthText.' '.$year.'<br>';
            }
        }

        $yr++;
    }
?>
于 2012-11-05T14:24:25.380 回答