如何以 12 小时格式创建 Php Timepicker
How to create Php Timepicker in 12 hrs format
我在我的项目中使用了 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>';
?>
您需要使用 date('h:i a')
来获得子午线 am
,pm
$end_time = strtotime('20:00');
$optionvalue = date("h:i a",$end_time);
echo $optionvalue;
/* 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">
<option>10:00am
<option>10:15am
<option>10:30am
<option>10:45am
<option>11:00am
<option>11:15am
<option>11:30am
<option>11:45am
<option>12:00pm
<option>12:15pm
<option>12:30pm
<option>12:45pm
<option>13:00pm
<option>13:15pm
<option>13:30pm
<option>13:45pm
<option>14:00pm
<option>14:15pm
<option>14:30pm
<option>14:45pm
<option>15:00pm
<option>15:15pm
<option>15:30pm
<option>15:45pm
<option>16:00pm
<option>16:15pm
<option>16:30pm
<option>16:45pm
<option>17:00pm
<option>17:15pm
<option>17:30pm
<option>17:45pm
<option>18:00pm
<option>18:15pm
<option>18:30pm
<option>18:45pm
<option>19:00pm
<option>19:15pm
<option>19:30pm
<option>19:45pm
<option>20:00pm
</select>
我在我的项目中使用了 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>';
?>
您需要使用 date('h:i a')
来获得子午线 am
,pm
$end_time = strtotime('20:00');
$optionvalue = date("h:i a",$end_time);
echo $optionvalue;
/* 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">
<option>10:00am
<option>10:15am
<option>10:30am
<option>10:45am
<option>11:00am
<option>11:15am
<option>11:30am
<option>11:45am
<option>12:00pm
<option>12:15pm
<option>12:30pm
<option>12:45pm
<option>13:00pm
<option>13:15pm
<option>13:30pm
<option>13:45pm
<option>14:00pm
<option>14:15pm
<option>14:30pm
<option>14:45pm
<option>15:00pm
<option>15:15pm
<option>15:30pm
<option>15:45pm
<option>16:00pm
<option>16:15pm
<option>16:30pm
<option>16:45pm
<option>17:00pm
<option>17:15pm
<option>17:30pm
<option>17:45pm
<option>18:00pm
<option>18:15pm
<option>18:30pm
<option>18:45pm
<option>19:00pm
<option>19:15pm
<option>19:30pm
<option>19:45pm
<option>20:00pm
</select>