1

我只包含了我的一些时间表脚本(处理日子的部分)

我注意到我的时间表一直运行良好,直到月底(每天都变成星期四),所以我回到了脚本,我相信我的时间表不知道什么时候检测它是否是一个新的月份,所以它继续行动就像一个不存在的计数器 31、32、33(一个月中的某天),这就是为什么它每天都给我一个星期四并说年份是 1970 年。

时间表显示从今天开始的 5 天 - 所以它会显示今天和其他 4 天,例如星期五、星期六、星期日和星期一。

我的代码如下:

       <?php
    }
    $timein = time();
    $d7 = date("H", $timein);
    $d6 = ($d7 +1) - 4;
    $d1 = date("d", $timein);

    $d2 = date("F", $timein);   
    $d3 = date("Y", $timein);
    $d4 = "$d2 $d3";
    $d5 = $d1 + 4;
    for( $i = $d1; $i <= $d5; $i++ ) {
    $day = strtotime( "{$i} {$d4}" );
    $day = date( "d/m/Y", $day );

    ?>
    <table border="0" cellspacing="0" cellpadding="0" width="20%" style="float: left;">
    <tr><td>
        <div class="square title" style="padding: 3px; -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px; width: 92%; text-align: center; bottom: 5px; margin-bottom: 5px; cursor: pointer; background: #79CDCD;">

            <strong><?php echo $day; ?></strong>

        </div>
</td></tr>
</table>
<? } ?>

是否有解决方案或更简单的方法来执行此类脚本?

提前谢谢你的帮助!

4

4 回答 4

1

你如何只增加 86400 秒$timein来增加你的一天?

于 2013-01-31T20:09:31.757 回答
1

您的代码似乎很难遵循。这通常不是一个好兆头。在处理日期时,我通常更喜欢更面向对象的方法:

$date = new DateTime();

for ($i = 0; $i <= 4; $i++) {
    ?>
    <table border="0" cellspacing="0" cellpadding="0" width="20%" style="float: left;">
    <tr><td>
        <div class="square title" style="padding: 3px; -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px; width: 92%; text-align: center; bottom: 5px; margin-bottom: 5px; cursor: pointer; background: #79CDCD;">

            <strong><?php echo $date->format('d/m/Y'); ?></strong>

        </div>
    </td></tr>
    </table>
    <?php
    $date->add(new DateInterval('P10D'));
}
于 2013-01-31T20:23:19.543 回答
0

正如 njk 建议的那样,您也可以通过添加 86400 秒的倍数来做到这一点。

<?php

for( $i = 0; $i < 5; $i++ ) {
    $day = time() + ($i * 86400);
    $day = date( "d/m/Y", $day );

    ?>
    <table border="0" cellspacing="0" cellpadding="0" width="20%" style="float: left;">
        <tr>
            <td>
                <div class="square title" style="padding: 3px; -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px; width: 92%; text-align: center; bottom: 5px; margin-bottom: 5px; cursor: pointer; background: #79CDCD;">
                    <strong><?php echo $day; ?></strong>
                </div>
            </td>
        </tr>
    </table>
<? } ?>
于 2013-01-31T20:18:25.787 回答
0

您可以使用strtotime获取今天午夜的值,并$i在 for 循环中添加天数

<?php

    for( $i = 0; $i < 5; $i++ ) {
        $day = strtotime( "today + $i days" );
        $day = date( "d/m/Y", $day );

        ?>
        <table border="0" cellspacing="0" cellpadding="0" width="20%" style="float: left;">
            <tr>
                <td>
                    <div class="square title" style="padding: 3px; -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px; width: 92%; text-align: center; bottom: 5px; margin-bottom: 5px; cursor: pointer; background: #79CDCD;">
                        <strong><?php echo $day; ?></strong>
                    </div>
                </td>
            </tr>
        </table>
<? } ?>
于 2013-01-31T20:11:35.940 回答