【问题标题】:print time between 10pm to 3am打印时间为晚上 10 点至凌晨 3 点
【发布时间】:2017-01-29 05:26:04
【问题描述】:

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

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 点。如何在这些字符串之间一直回显?

【问题讨论】:

    标签: php codeigniter time timestamp do-while


    【解决方案1】:

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

    <?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 时循环

    【讨论】:

    • 它将进入无限循环
    • 问题是10pm已经比3am大,所以不会循环
    • 是的,但我想打印它们之间的时间。有什么想法吗?
    • 我怎样才能在这一行中将此$close_time = strtotime(date('Y-m-d H:i',strtotime(date('Y-m-d',strtotime('+1 day')).'3am'))); 3 am 更改为动态?
    • 所以只需将变量放入您的值而不是'3am'
    【解决方案2】:

    我希望这是您正在寻找的:

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

    【讨论】:

      【解决方案3】:

      试试这个

      $new_otime = "10pm";
      $close_time = "3am";
      $counter=0;
      while(date("g a",strtotime($new_otime)+3600*$counter)<=date("g a",strtotime($close_time))){
           echo date("g a",strtotime($new_otime)+3600*$counter).'<br>';
           $counter++;
      
      }
      

      【讨论】:

        【解决方案4】:

        试试这个代码

            <?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 } ?>
        

        【讨论】:

          【解决方案5】:

          您也可以尝试使用 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');
          }
          

          【讨论】:

            猜你喜欢
            • 2020-04-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-01-27
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多