Rahul Pamnani
Rahul Pamnani

Reputation: 1435

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;">
    <thead>
      <tr>
        <td style="border:1px solid;padding:11px;">Time</td>
        <td style="border:1px solid;padding:11px;">Book</td>
      </tr>
    </thead>
    <tbody>
    <?php 
        $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) { 
  ?>  
       <tr>
        <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>
      </tr>
      <?php } ?>

      </tbody>
  </table>

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

Upvotes: 1

Views: 690

Answers (3)

evansgambit
evansgambit

Reputation: 905

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

Upvotes: 1

momouu
momouu

Reputation: 711

Try this

$arr_ctr=count($array_of_time)-1;
  foreach ($array_of_time as $key => $single_time) { 
    if($arr_ctr>$key){
  ?>  
       <tr>
        <td style="border:1px solid;padding:11px;"><?php echo $array_of_time[$key].' - '.$array_of_time[$key+1]; ?><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>
      </tr>
      <?php 
     }
    } ?>

Add a in the date() for the am and pm

$array_of_time[] = date ("h:i a", $start_time);

Upvotes: 1

Sasikumar
Sasikumar

Reputation: 863

Use the current() and next() in foreach as follows to get the next element

foreach ($array_of_time as $key => $single_time) {
   echo current($array_of_time); 
   echo " => ";
   echo next($array_of_time); 
   echo '<br>';
}

Upvotes: 1

Related Questions