打印时间为晚上10点至凌晨3点

时间:2016-09-21 05:51:44

标签: php codeigniter time timestamp do-while

我有两个时间字符串如下:

10pm
3am

我想在上面给出的字符串之间打印时间,如:

10pm
11pm
12am
1am
2am
3am

我试过的PHP代码:

<?php
$new_otime = strtotime(date('H:i',strtotime('10pm')));
$close_time = strtotime(date('H:i',strtotime("3am")));
do { ?>
<option value="<?php echo date("g a",$new_otime) ?>"><?php echo date("g a",$new_otime); ?></option>
<?php
$new_otime = $new_otime+=3600;                      
} while ($new_otime == $close_time);
?>

但它在选择框中仅在晚上10点回响。如何在这些字符串之间回显所有时间?

5 个答案:

答案 0 :(得分:2)

我建议做一些工作来完成这项工作:

<?php
$new_otime = strtotime(date('Y-m-d H:i',strtotime(date('Y-m-d').' 10pm')));
$close_time = strtotime(date('Y-m-d H:i',strtotime(date('Y-m-d').' 3am')));
if($close_time < $new_otime){
  $close_time = strtotime(date('Y-m-d H:i',strtotime(date('Y-m-d',strtotime('+1 day')).'3am')));
}
do { ?>
<option value="<?php echo date("g a",$new_otime) ?>"><?php echo date("g a",$new_otime); ?></option>
<?php
$new_otime = $new_otime+=3600;                      
} while ($new_otime <= $close_time);
?>
  1. 检查结束时间是否更大
  2. 如果不是第二天再拍的话
  3. new_otime小于close_time
  4. 时循环播放

答案 1 :(得分:2)

您也可以尝试使用DateTime和DatePeriod:

$from = date_create_from_format('ha', '10pm');
//create 5 interations, each adds an additional hour
$dp = new DatePeriod($from, new DateInterval('PT1H'), 5);

/** @var DateTime $d */
foreach ($dp as $d) {
    echo $d->format('ha');
}

答案 2 :(得分:0)

我希望这就是你要找的东西:

$time=22;
for($i=0;$i<6;$i++) {
echo date("g a",mktime($time+$i,0,0,09,21,2016))."<br>";
}

答案 3 :(得分:0)

试试这个

[row, col] = ind2sub([size(c,1), size(c,2)],find(logical_result == 1));
result = c(min(row):max(row),min(col):max(col));
imshow(result);

答案 4 :(得分:0)

试试此代码

    <?php
    $new_otime = "10pm";
    $close_time = "3am";
    $new_otime = strtotime(date_create_from_format("ha", $new_otime)->format("ha"));
    $close_time = strtotime(date_create_from_format("ha", $close_time)->format("ha"));
        do {
            ?>
            <option value="<?php echo date("ha", $new_otime) ?>"><?php echo date("ha", $new_otime); ?></option>
            <?php
            $new_otime = strtotime(date("ha", strtotime("+1 hours", $new_otime)));
        } while ($new_otime != $close_time);
        if ($new_otime == $close_time) {
            ?>
            <option value="<?php echo date("ha", $new_otime) ?>"><?php echo date("ha", $new_otime); ?></option>
    <?php } ?>