2013-01-31 15 views
1

我只包括了一些我的時間表腳本(即與日交易的一部分)PHP時間表一天問題

我發現我的時間表工作正常,直到月底(日常成了星期四)和所以我回到腳本中,我相信我的時間表不知道何時檢測它是否是新月,所以它繼續像一個不存在的計數器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> 
<? } ?> 

是否有解決方案或更簡單的方法也許做這樣的腳本?

非常感謝您的幫助!

回答

1

你剛剛加了86400秒到$timein來增加你的一天嗎?

+0

您忘記了夏令時。 – Mike

+0

'time()'是GMT。 – Kermit

+0

你說得對。沒關係。 – Mike

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