php - Excluding specific half hour from the list of half hours -
i have loop gives outcome of number of half hours between given start time , end time
$start = new datetime('09:00:00'); // add 1 second because last 1 not included in loop $end = new datetime('16:00:01'); $interval = new dateinterval('pt30m'); $period = new dateperiod($start, $interval, $end); $previous = ''; foreach ($period $dt) { $current = $dt->format("h:ia"); if (!empty($previous)) { echo "<input name='time' type='radio' value='{$previous}|{$current}'> {$previous}-{$current}<br/>"; } $previous = $current; }
the outcome of above loop following
09:00am-09:30am 09:30am-10:00am 10:00am-10:30am 10:30am-11:00am 11:00am-11:30am 11:30am-12:00pm 12:00pm-12:30pm 12:30pm-01:00pm 01:00pm-01:30pm 01:30pm-02:00pm 02:00pm-02:30pm 02:30pm-03:00pm 03:00pm-03:30pm 03:30pm-04:00pm
what trying achieve exclude of time mentioned below if exist in array $existing_time
looks this
[0] => array ( [start_time] => 2014-03-28t14:00:00+1100 [end_time] => 2014-03-28t14:30:00+1100 ) [1] => array ( [start_time] => 2014-03-28t15:00:00+1100 [end_time] => 2014-03-28t15:30:00+1100 ) )
i need in how go doing this, appreciated
i added start times array , check see if current start time in array. if so, skip it.
<?php $start = new datetime('09:00:00'); $end = new datetime('16:00:01'); // add 1 second because last 1 not included in loop $interval = new dateinterval('pt30m'); $period = new dateperiod($start, $interval, $end); $existing_time = array( array( 'start_time' => '2014-03-28t14:00:00+1100', 'end_time' => '2014-03-28t14:30:00+1100' ), array( 'start_time' => '2014-03-28t15:00:00+1100', 'end_time' => '2014-03-28t15:30:00+1100' ) ); $booked = array(); foreach ($existing_time $ex) { $dt = new datetime($ex['start_time']); $booked[] = $dt->format('h:ia'); } $previous = ''; foreach ($period $dt) { $current = $dt->format("h:ia"); if (!empty($previous) && !in_array($previous, $booked)) { echo "<input name='time' type='radio' value='{$previous}|{$current}'> {$previous}-{$current}<br/>"; } $previous = $current; }
Comments
Post a Comment