Loop Issue Counting Year In PHP

后端 未结 10 1189
陌清茗
陌清茗 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 10:52

    You need to use the str_pad function (manual). In your case, it goes like this:

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

    Why go through such a long and odd process, when you can do something like this?

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

    Output:

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

    This also has the added bonus of not showing "31 Feb 2008" etc., as that date doesn't even exist.

    Codepad example of the code (WARNING: long output!)

    0 讨论(0)
  • 2021-01-22 11:02

    Easiest fix is to set $yr = 2004 and loop while $yr < 2014. You are not padding your numbers with a leading zero, hence 204, 205, etc.

    0 讨论(0)
  • 2021-01-22 11:08

    This is not standard, but you can add an if loop inside that while loop like this:

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

    While you'd wanna do something like this beats me though

    0 讨论(0)
  • 2021-01-22 11:08

    It's because you're setting $yr like that:

    $yr = 4;
    

    Try this:

    $yr = sprintf('%02d', $yr);
    echo "$x Jan 20$yr<br>";
    
    0 讨论(0)
  • 2021-01-22 11:09

    Use str_pad:

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

    Is more appropriate to use the function cal_days_in_month of the variable $x:

    <?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++;
        }
    ?>
    
    0 讨论(0)
提交回复
热议问题