时间列表从上午12点到上午12点

时间:2014-09-19 08:43:13

标签: php

我希望将时间从00:00打印到23.30,间隔为15 minute。与00.0000.1500.30等相同。我该怎么办?

有人可以帮我吗?

代码:

<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分钟给我一次输出。我想用same logic每15分钟更改一次。我该怎么做?

7 个答案:

答案 0 :(得分: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>";

它更干净,更短,更容易修改。

答案 1 :(得分:1)

<?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>";
 ?>

答案 2 :(得分: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";}}}
                    ?>

答案 3 :(得分: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,则可以修改数组,以便小时和分钟为零填充,从而无需使用{{1} }陈述。

还应该将if检查移到in_array之外,以避免创建56个空白行。

答案 4 :(得分: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>

答案 5 :(得分: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>

答案 6 :(得分: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++;
}
?>