2014-09-19 119 views
-1

我想要打印時間從00:0023.30,間隔時間爲15 minute。像00.0000.15,00.30等。我該怎麼做 ?時間表從上午12點到上午12點

任何人都可以幫助我嗎?

代碼:

<table border=2> 
<?php 
$time = 23; 
$k=0;  
for ($i = 12; $i <= $time; $i++) { 
     for ($min = 0; $min < 2; $min++) { 
      ?> 
      <tr>   
       <td><?php 
        if (($min % 2) == 0) { 
         if (strlen($i) == 1) 
          echo $str1 = "0" . $i . ":00"; 
         else 
          echo $str1 = $i . ":00"; 
        }else{ 
        if (strlen($i) == 1) 
         echo $str1 = "0" . $i . ":30"; 
        else 
         echo $str1 = $i . ":30";}}} 
        ?></td></tr> 

這是給我的輸出每30個minute.I想我。如何能做到這一點的改變這種每15分鐘與same logic

+0

你明白你發佈的代碼? – mafu 2015-04-14 10:05:56

+0

代碼有問題嗎? – Twix 2015-04-14 10:21:18

回答

0

嘗試,如果你從00:00開始$i = 0

<?php 
$time = 23; 
$k=0;  
for ($i = 12; $i <= $time; $i++) { 
     for ($min = 0; $min < 4; $min++) { 
      ?> 
      <tr>   
       <td><?php 
        if (($min % 4) == 0) { 
         if (strlen($i) == 1) 
          echo $str1 = "0" . $i . ":00"; 
         else 
          echo $str1 = $i . ":00"; 
        } 
        elseif (($min % 3) == 0) { 
         if (strlen($i) == 1) 
          echo $str1 = "0" . $i . ":45"; 
         else 
          echo $str1 = $i . ":45"; 
        } 
        elseif (($min % 2) == 0) { 
         if (strlen($i) == 1) 
          echo $str1 = "0" . $i . ":30"; 
         else 
          echo $str1 = $i . ":30"; 
        } 

        else{ 
        if (strlen($i) == 1) 
         echo $str1 = "0" . $i . ":15"; 
        else 
         echo $str1 = $i . ":15";}}} 
        ?> 
11
$time = new \DateTime("midnight"); 
    $endTime = new \DateTime("midnight tomorrow"); 

    echo "<table border=2>"; 
    while ($time < $endTime) { 
     echo "<tr><td>" . $time->format("H:i") . "</td></tr>"; 
     $time->modify("+15 minutes"); 
    } 
    echo "</table>"; 

它更乾淨,更短,FAR更容易修改。

+0

「+1」多數民衆贊成在美! – mithunsatheesh 2014-09-19 08:53:25

+0

非常好的答案和風格 – DKSan 2014-09-19 08:54:50

+0

具有相同的邏輯? – 2014-09-19 08:55:45

3
<?php 
$start_time = "00:00:00"; 
$end_time = "23:45:00"; 
echo "<table>"; 
for($time = strtotime($start_time); $time <= strtotime($end_time); $time += 900) { 
?> 
    <tr><td><?php echo date("H:i",$time)."<br/>";?></td></tr> 
<?php } 
echo "</table>"; 
?> 
0

我的嘗試,這是剛剛整理現有的邏輯想:

<?php 

$time = 23; 
$k = 0; // not used anywhere? 
$mins = [0, 15, 30, 45]; 

for ($i = 12; $i <= $time; $i++) { 
    for ($min = 0; $min <= 60; $min++) { 
     ?> 
     <tr>   
      <td> 
       <?php 
        if (in_array($min, $mins)) { 
         echo $str1 = str_pad($i, 2, "0", STR_PAD_LEFT) . ":" . str_pad($min, 2, "0", STR_PAD_LEFT); 
        } 

    } 
} 

?> 

所以你可以修改數組如果您想再次添加並且使用str_pad,以便小時和分鐘爲零填充,從而不需要使用if語句中的幾個。

也應該將in_array支票移動到tr之外,以避免創建56個空白行。

0
<table border=2> 
    <?php 
    $time = 23; 
    for ($i = 12; $i <= $time; $i++) { 
     for ($min = 0; $min < 4; $min++) { 
      ?> 
      <tr>   
       <td> 
        <?php 
        switch ($min) { 
         case '0': echo $i . ':00'; 
          break; 
         case '1': echo $i . ':15'; 
          break; 
         case '2': echo $i . ':30'; 
          break; 
         case '3': echo $i . ':45'; 
          break; 
        } 
        ?> 
       </td> 
      </tr> 
      <?php 
     } 
    } 
    ?> 
</table> 
0
<table border=2> 
<?php 
$time = 23; 
$k=0;  
for ($i = 12; $i <= $time; $i++) { 
     for ($min = 0; $min < 4; $min++) { 
      ?> 
      <tr>   
       <td><?php 
        if (($min % 4) == 0) { 
         if (strlen($i) == 1) 
          echo $str1 = "0" . $i . ":00"; 
         else 
          echo $str1 = $i . ":00"; 
        }else if (($min % 4) == 1){ 
         if (strlen($i) == 1) 
          echo $str1 = "0" . $i . ":15"; 
         else 
          echo $str1 = $i . ":15"; 
        }else if (($min % 4) == 2){ 
         if (strlen($i) == 1) 
          echo $str1 = "0" . $i . ":30"; 
         else 
          echo $str1 = $i . ":30"; 
        }else{ 
         if (strlen($i) == 1) 
          echo $str1 = "0" . $i . ":45"; 
         else 
          echo $str1 = $i . ":45"; 
        } 


        }} 
        ?> 
</td></tr> 
+0

你可以添加幾行來解釋你的解決方案?謝謝。 – m4rtin 2014-09-19 09:55:23

0

這是我嘗試

<table border=2> 
<?php 
$h = 0; 
$m = 0; 
while($h <= 23) 
{ 
    while($m < 60) 
    { 
     $hour = sprintf('%02d',$h); 
     $minute = sprintf('%02d',$m); 
     print "<tr><td>$hour:$minute</td></tr>"; 
     $m += 15; 
    } 
    $m = 0; 
    $h++; 
} 
?> 
相關問題