Loop Issue Counting Year In PHP

后端 未结 10 1190
陌清茗
陌清茗 2021-01-22 10:35

I have created a loop which will display date 2004 to 2014 in a formatted way. But the problem is, it is showing 204 instead of 2004 and continue this till 209.. So, how to show

相关标签:
10条回答
  • 2021-01-22 11:14

    According to your code, you can try this. Though its not a standard way:

    <?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++;
    }
    ?>
    
    0 讨论(0)
  • 2021-01-22 11:16

    You could do it also with a (Quite) different Structure:

    <?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';
        }
    ?>
    
    0 讨论(0)
  • 2021-01-22 11:18

    All you need is one loop

    $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");
    }
    
    0 讨论(0)
  • 2021-01-22 11:18

    I am not sure why are you doing this by so many loops, use this instead of $yr and you will get the correct year printed:

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

    Best

    0 讨论(0)
提交回复
热议问题