假设我有一个包含此类的多个实例的数组:
class Booking
{
public $status;
public $start_time;
public $end_time;
}
array[0] $status = true $start_time = 13:30 $end_time = 14:00
array[1] $status = true $start_time = 14:30 $end_time = 17:30
array[2] $status = true $start_time = 20:30 $end_time = 21:30
array[3] $status = true $start_time = 22:00 $end_time = 23:30
我怎么能详细说明一个给定时间的函数,比如12:00
和23:00
会生成一个数组,同一个类的多个实例,尽管有这样的东西:
array[0] $status = false $start_time = 12:00 $end_time = 13:30
array[1] $status = true $start_time = 13:30 $end_time = 14:00
array[2] $status = false $start_time = 14:00 $end_time = 14:30
array[3] $status = true $start_time = 14:30 $end_time = 17:30
array[4] $status = false $start_time = 17:30 $end_time = 20:30
array[5] $status = true $start_time = 20:30 $end_time = 21:30
array[6] $status = false $start_time = 21:30 $end_time = 22:00
array[7] $status = true $start_time = 22:00 $end_time = 23:00
答案 0 :(得分:1)
朝这个方向看:
function generate($from, $till)
{
$result = array();
$t = $from;
$status = true;
while($t <= $till)
{
$result[] = new Booking($status, date('h:i', $t), date('h:i', $t+30*60));
$t = $t+30*60;
$status = !$status;
}
return $result;
}