Sarathlal N

Split time in to time slots - PHP

If you need to split time into time slots with a proper time interval, you can use the below code snippet.

function prepare_time_slots($starttime, $endtime, $duration){
	 
	$time_slots = 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
	{
	   $time_slots[] = date("H:i", $start_time);
	   $start_time += $add_mins; // to check endtime
	}

	return $time_slots;
}

Example

$starttime = '9:00';  // your start time
$endtime = '21:00';  // End time
$duration = '30';  // split by 30 mins

$time_slots = prepare_time_slots($starttime, $endtime, $duration);

echo '<pre>';
print_r($time_slots);
echo '</pre>';

Output

Array
(
	[0] => 09:00
	[1] => 09:30
	[2] => 10:00
	[3] => 10:30
	[4] => 11:00
	[5] => 11:30
	[6] => 12:00
	[7] => 12:30
	[8] => 13:00
	[9] => 13:30
	[10] => 14:00
	[11] => 14:30
	[12] => 15:00
	[13] => 15:30
	[14] => 16:00
	[15] => 16:30
	[16] => 17:00
	[17] => 17:30
	[18] => 18:00
	[19] => 18:30
	[20] => 19:00
	[21] => 19:30
	[22] => 20:00
	[23] => 20:30
	[24] => 21:00
)

Got a project in mind? Send me a quick message, and I'll get back to you within 24 hours!.

Recent Posts

  1. Disabling Payment Methods in WooCommerce Based on Conditions
  2. How to Update Product Quantity in WooCommerce Using Custom Code
  3. Dynamically Generating a Table of Contents in WordPress
  4. Direct Checkout in WooCommerce - Add Product to Cart from Checkout Page & Skip Shop, Product, and Cart Pages
  5. Understanding the Impact of git reset --hard Command

Your Questions / Comments

If you found this article interesting, found errors, or just want to discuss about it, please get in touch.