Rahul Pamnani Rahul Pamnani - 11 months ago 58
HTML Question

I want to show timing by the interval of 30 minutes with am or pm

I am working on some booking system in which availability timing is 10:00 am to 5:00 pm.I am showing this time by the interval of 30 minutes in the table.10:00 am is the start time and 5:00 pm is the end time.But I am able to show timing by the interval of 30 minutes in next row like 10:00 in first row.10:30 in the second row and so on.But I want 10:00 am - 10:30 am in row row.10:30 am - 11:00 am in the second row.

<table style="border: 1px solid;">
<td style="border:1px solid;padding:11px;">Time</td>
<td style="border:1px solid;padding:11px;">Book</td>
$starttime = $query2['starttime']; // your start time
$endtime = $query2['endtime']; // End time
$duration = '30'; // split by 30 mins

$array_of_time = array ();
$start_time = strtotime ($starttime); //change to strtotime
$end_time = strtotime ($endtime); //change to strtotime

$add_mins = $duration * 60;
while ($start_time <= $end_time) // loop between time
$array_of_time[] = date ("h:i", $start_time);
$start_time += $add_mins; // to check endtime

foreach ($array_of_time as $key => $single_time) {
<td style="border:1px solid;padding:11px;"><?php echo $single_time; ?><br /><span style="color:red;"><?php echo "Available"; ?></span></td>
<td style="border:1px solid;padding:11px;"><input type="radio" value="<?php echo $single_time; ?>" id="A" name="A" ></td>
<?php } ?>


I want to show timing like 10:00 am - 10:30 am in the first row and so on.

Answer Source

You can use the DateTime object. This way you can modify it later by just replacing the value in the DateTime constructor.

$date = new DateTime('2016-09-17 10:00');

while ( $start_time < $end_time )
    $start = $date->format('H:i');

    $date->add(new DateInterval('PT30M'));

    echo $start . ' - ' . $date->format('H:i') . "\n";

    $start_time += $add_mins;