这似乎测试正常。http://www.laprbass.com/RAY_temp_daan.php
<?php // RAY_temp_daan.php
error_reporting(E_ALL);
/* PROBLEM DEFINITION
The timeslots are the same for each day: from 9 to 13, 13 to 17, and 19 to 23 hrs.
For example, say I have a start time of 17-12-2012 13:00, and an end time of 18-12-2012 11:00, I want my function to give me an array like this:
array(1) {
["meetingroom1_booked"]=>
array(3) {
[0]=>
array(2) {
["day"]=>
string(10) "2012-12-17"
["timeslot"]=>
string(1) "2"
}
[1]=>
array(2) {
["day"]=>
string(10) "2012-12-17"
["timeslot"]=>
string(1) "3"
}
[2]=>
array(2) {
["day"]=>
string(10) "2012-12-18"
["timeslot"]=>
string(1) "1"
}
}
}
*/
// THE TIMEZONE
date_default_timezone_set('America/Chicago');
// THE START AND END TIMES
$alpha = '2012-12-17 13:00';
$omega = '2012-12-18 11:00';
// THE DEFINITION OF THE TIMESLOTS
$slots = array
( 1 => array
( 'lo' => '0900'
, 'hi' => '1259'
)
, 2 => array
( 'lo' => '1300'
, 'hi' => '1659'
)
, 3 => array
( 'lo' => '1900'
, 'hi' => '2259'
)
)
;
// MAKE AN ARRAY OF TIMESTAMPS OF THE HOURS
$tsa = strtotime($alpha);
$tsz = strtotime($omega);
$hours = range($tsa, $tsz, 3600);
// ASSIGN EACH HOUR TO A SLOT
foreach ($hours as $hour)
{
$hhmm = date('Hi', $hour);
foreach ($slots as $slot => $times)
{
if ($hhmm >= $times['lo'])
{
if ($hhmm <= $times['hi'])
{
// USE STRING NOTATION HERE SO WE CAN USE ARRAY_UNIQUE()
$taken[$hour] = date('Y-m-d', $hour) . '|' . $slot;
}
}
}
}
// REMOVE THE REDUNDANCIES
$taken = array_unique($taken);
// CONSTRUCT THE ASSOCIATIVE ARRAYS
foreach($taken as $thing)
{
$arr = explode('|', $thing);
$out[] = array('day' => $arr[0], 'timeslot' => $arr[1]);
}
// CONSTRUCT THE FINAL ARRAY
$out = array("meetingroom1_booked" => $out);
// SHOW THE WORK PRODUCT
echo '<pre>';
var_dump($out);