显示变量中给定的每个数字

时间:2016-07-13 12:30:40

标签: php date

我有一点问题,我正在尝试制作一个日历,然后我可以根据数据库的结果更改每个框的颜色。目前,我的代码给了我一年中有多少天。有没有一种方法我可以说,例如30个盒装,每个数字上升到30,这样我就可以选择一个特定数量的盒子然后使它变成某种颜色。

  <?php
$number = cal_days_in_month(CAL_GREGORIAN, 2, 2003); 
?>

我已经尝试过使用从数据库中的行中获取信息的方法,这显然是行不通的。 亲切的问候 千斤顶

1 个答案:

答案 0 :(得分:0)

我们希望,诚挚地,我们在这里同步。人们可以认为他理解你的意思,但那可能不那么精确。无论如何,您可以使用loop循环$number通过变量for(){},然后创建具有唯一<div>的框(可能是HTML css classes元素)来帮助你根据自己的意愿style。这是一个可能的想法:

    <?php
        $number     = cal_days_in_month(CAL_GREGORIAN, 2, 2003);

        // LOOP THROUGH THE NUMBER OF MONTHS USING FOR-LOOP AND CREATE
        // THE BOXES THAT YOU DESIRE (WITH CSS-CLASSES, THOUGH)
        $calBoxes   = "";
        for($cue=1; $cue<=$number; $cue++){
            $fmtDay    = date("D", strtotime("2003-2-{$cue}"));
            $calBoxes .= "<div class='date-box date-box-{$cue}'>" . PHP_EOL;
            $calBoxes .= "<span class='date-number date-number-{$cue}'>{$fmtDay}</span>" . PHP_EOL;
            $calBoxes .= "<span class='weekday weekday-{$cue} weekday-" . strtolower($fmtDay) . "'>{$cue}</span>" . PHP_EOL;
            $calBoxes .= "</div>" . PHP_EOL;
        }
        echo $calBoxes; //<== DIRECTLY OUTPUTS THE DATA

        // JUST FOR TESTING PURPOSES; YOU MAY SIMPLY DO A var_dump();
        // var_dump($calBoxes);
        // PRODUCES...          
        <div class='date-box date-box-1'>
            <span class='date-number date-number-1'>Sat</span>
            <span class='weekday weekday-1 weekday-sat'>1</span>
        </div>
        <div class='date-box date-box-2'>
            <span class='date-number date-number-2'>Sun</span>
            <span class='weekday weekday-2 weekday-sun'>2</span>
        </div>
        <div class='date-box date-box-3'>
            <span class='date-number date-number-3'>Mon</span>
            <span class='weekday weekday-3 weekday-mon'>3</span>
        </div>
        <div class='date-box date-box-4'>
            <span class='date-number date-number-4'>Tue</span>
            <span class='weekday weekday-4 weekday-tue'>4</span>
        </div>
        <div class='date-box date-box-5'>
            <span class='date-number date-number-5'>Wed</span>
            <span class='weekday weekday-5 weekday-wed'>5</span>
        </div>
        <div class='date-box date-box-6'>
            <span class='date-number date-number-6'>Thu</span>
            <span class='weekday weekday-6 weekday-thu'>6</span>
        </div>
        <div class='date-box date-box-7'>
            <span class='date-number date-number-7'>Fri</span>
            <span class='weekday weekday-7 weekday-fri'>7</span>
        </div>
        <div class='date-box date-box-8'>
            <span class='date-number date-number-8'>Sat</span>
            <span class='weekday weekday-8 weekday-sat'>8</span>
        </div>
        <div class='date-box date-box-9'>
            <span class='date-number date-number-9'>Sun</span>
            <span class='weekday weekday-9 weekday-sun'>9</span>
        </div>
        <div class='date-box date-box-10'>
        <span class='date-number date-number-10'>Mon</span>
        <span class='weekday weekday-10 weekday-mon'>10</span>
        </div>
        <div class='date-box date-box-11'>
            <span class='date-number date-number-11'>Tue</span>
            <span class='weekday weekday-11 weekday-tue'>11</span>
        </div>
        <div class='date-box date-box-12'>
            <span class='date-number date-number-12'>Wed</span>
            <span class='weekday weekday-12 weekday-wed'>12</span>
        </div>
        <div class='date-box date-box-13'>
            <span class='date-number date-number-13'>Thu</span>
            <span class='weekday weekday-13 weekday-thu'>13</span>
        </div>
        <div class='date-box date-box-14'>
            <span class='date-number date-number-14'>Fri</span>
            <span class='weekday weekday-14 weekday-fri'>14</span>
        </div>
        <div class='date-box date-box-15'>
            <span class='date-number date-number-15'>Sat</span>
            <span class='weekday weekday-15 weekday-sat'>15</span>
        </div>...

希望这能给你(至少)一点线索......

干杯; - )