如何以 12 小时格式创建 Php Timepicker

我在我的项目中使用了 php timepicker,它是 24 小时格式,但在新要求中,我只需要实现 12 小时格式,因为我必须检查 AM PM 格式的时间以与传递的变量值进行比较。那么以 12 小时格式创建的过程是什么


    <?php 

      $time = strtotime('10:00');


      $end_time = strtotime('20:00');

      $end_time = date("h:i a",$end_time);


        echo '<select>';

        while($time <= $end_time){

            $optionvalue = date("H:i",$time);

            echo '<option value="'.$optionvalue 

              .'">'.$optionvalue.'</option>';

            $time = strtotime('+15 minutes',$time);

        }

        echo '</select>';


    ?>


慕妹3242003
浏览 123回答 2
2回答

墨色风雨

您需要使用date('h:i a')meridiem am,pm$end_time = strtotime('20:00');$optionvalue = date("h:i a",$end_time);echo $optionvalue;

MM们

/* start and end times converted to DateTime objects */$start=new DateTime( date( DATE_ATOM, strtotime('10am') ) );$end=new DateTime( date( DATE_ATOM, strtotime('8pm') ) );$interval=new DateInterval('PT15M');/* ensure the initial time is part of the output */$start->sub( $interval );$slots=array();while( $start->add( $interval ) <= $end )$slots[]=$start->format('H:ia');printf('<select name="times"><option>%s</select>', implode( '<option>', $slots ) );document.querySelector('select[name="times"]').onchange=(e)=>{alert( e.target.value )}<select name="times">&nbsp; &nbsp; <option>10:00am&nbsp; &nbsp; <option>10:15am&nbsp; &nbsp; <option>10:30am&nbsp; &nbsp; <option>10:45am&nbsp; &nbsp; <option>11:00am&nbsp; &nbsp; <option>11:15am&nbsp; &nbsp; <option>11:30am&nbsp; &nbsp; <option>11:45am&nbsp; &nbsp; <option>12:00pm&nbsp; &nbsp; <option>12:15pm&nbsp; &nbsp; <option>12:30pm&nbsp; &nbsp; <option>12:45pm&nbsp; &nbsp; <option>13:00pm&nbsp; &nbsp; <option>13:15pm&nbsp; &nbsp; <option>13:30pm&nbsp; &nbsp; <option>13:45pm&nbsp; &nbsp; <option>14:00pm&nbsp; &nbsp; <option>14:15pm&nbsp; &nbsp; <option>14:30pm&nbsp; &nbsp; <option>14:45pm&nbsp; &nbsp; <option>15:00pm&nbsp; &nbsp; <option>15:15pm&nbsp; &nbsp; <option>15:30pm&nbsp; &nbsp; <option>15:45pm&nbsp; &nbsp; <option>16:00pm&nbsp; &nbsp; <option>16:15pm&nbsp; &nbsp; <option>16:30pm&nbsp; &nbsp; <option>16:45pm&nbsp; &nbsp; <option>17:00pm&nbsp; &nbsp; <option>17:15pm&nbsp; &nbsp; <option>17:30pm&nbsp; &nbsp; <option>17:45pm&nbsp; &nbsp; <option>18:00pm&nbsp; &nbsp; <option>18:15pm&nbsp; &nbsp; <option>18:30pm&nbsp; &nbsp; <option>18:45pm&nbsp; &nbsp; <option>19:00pm&nbsp; &nbsp; <option>19:15pm&nbsp; &nbsp; <option>19:30pm&nbsp; &nbsp; <option>19:45pm&nbsp; &nbsp; <option>20:00pm</select>
打开App,查看更多内容
随时随地看视频慕课网APP