2012-12-17 57 views
4

我需要您的幫助,解決以下問題。我正在建立一個使用PHP的網站,我想在這個網站上顯示一個顯示星期幾的日曆,每天細分爲3個時隙。這些時隙可以是免費的或預訂的,在這種情況下該時隙是不可選擇的。基於PHP的開始和結束時間返回時隙數組

到目前爲止這麼好,但問題在於按預訂顯示時隙。我從腳本中獲取預訂數據,該腳本給了我一個開始日期和時間以及結束日期和時間。這些日期/時間戳可以是任何事物,並且不受時隙時間的限制。他們可以在同一天或跨越多天。每天的時間段相同:從9日到13日,13日至17日和19日至23日。

例如,假設我的開始時間是17-12-2012 13:00,結束時間是18-12-2012 11:00,我希望我的函數給我這樣一個數組:

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

我一直在絞盡腦汁,但不能提出一個很好的解決方案來處理開始時間跨度多天。在一天的開始和結束時間,這不成問題:我可以比較開始時間小時和時間小時數。儘管當然這不適用於多天。任何指向正確的方向的高度讚賞!謝謝!

+0

看一看這篇文章,而我在你的數據看這裏。 http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html –

+0

非常感謝很多人的幫助,修復了它! –

回答

2

另一種方法用簡單的算法:

<?php 
$start_date = '2012-12-17'; 
$start_time = '13:00'; 
$end_date = '2012-12-18'; 
$end_time = '11:00'; 
$slots = array(1=>'09:00', 2=>'13:00', 3=>'19:00'); 

// [helper] 
$start_date_stamp = strtotime($start_date); 
$end_date_stamp = strtotime($end_date); 
$startdate_timestamp = strtotime($start_date . ' ' . $start_time); 
$enddate_timestamp = strtotime($end_date . ' ' . $end_time); 
// [/helper] 

$cur_date_stamp = $start_date_stamp; 
$result = array(); 
if ($enddate_timestamp >= $startdate_timestamp) { 
    // [loop for each date] 
    while($cur_date_stamp <= $end_date_stamp) { 
     foreach($slots as $slkey=>$slval) { 
      if ($cur_date_stamp == $start_date_stamp) { // compare for current date = start date 
       $check_slot_stamp = strtotime(date('Y-m-d', $cur_date_stamp) . ' ' . $slval); 
       if ($startdate_timestamp <= $check_slot_stamp) { 
        $result[] = array(
         'day' => date('Y-m-d', $cur_date_stamp), 
         'timeslot' => $slkey 
        ); 
       } 
      } 
      elseif ($cur_date_stamp == $end_date_stamp) { // compare for current date = end date 
       $check_slot_stamp = strtotime(date('Y-m-d', $cur_date_stamp) . ' ' . $slval); 
       if ($check_slot_stamp <= $enddate_timestamp) { 
        $result[] = array(
         'day' => date('Y-m-d', $cur_date_stamp), 
         'timeslot' => $slkey 
        ); 
       } 
      } 
      else { // 3 slots will be added for any date other than start_date and end_date 
       $result[] = array(
        'day' => date('Y-m-d', $cur_date_stamp), 
        'timeslot' => $slkey 
       ); 
      } 
     } 
     $cur_date_stamp = strtotime('+1 day', $cur_date_stamp); // next day 
    } 
    // [/loop for each date] 
} 
$result = array('meetingroom1_booked'=>$result); 
var_dump($result); 
+0

謝謝!使用大部分代碼,就像魅力一樣。 –

0

當您將時間轉換爲UNIX TIMESTAMP時,您可以簡單地執行此檢查。

當前時間戳爲:1355757162 時間戳明天5點:1355846439

每個時間戳大於1355757162和較小然後1355846439應該預訂。

1

這是一個通用的函數,我想出了可能有所幫助。我對'時間段'的計算可能與您所期望的完全不符,但您可以更新它以適應您的需求。

$res = calc('01-12-2012 09:00', '01-12-2012 13:00'); 
print_r($res); 
$res = calc('17-12-2012 13:00', '18-12-2012 11:00'); 
print_r($res); 

function calc($start, $end) { 
    // generic way to determine time slot; value is the high hour. 
    static $slots = array(1 => 13, 2 => 17, 3 => 23); 

    $oneDay = new \DateInterval("P1D"); 

    if (!($start instanceof \DateTime)) { 
     $start = date_create($start); 
    } 
    if (!($end instanceof \Datetime)) { 
     $end = date_create($end); 
    } 

    $ary = array(); 
    $now = clone $start; 
    do { 
     $res = array('day' => $now->format('Y-m-d')); 
     foreach ($slots as $idx => $hour) { 
      if ($now->format('H') <= $hour) { 
       $res['slot'] = $idx; 
       break; 
      } 
     } 
     $ary[] = $res; 

     $now->add($oneDay); 
    } while ($now->format('Y-m-d') <= $end->format('Y-m-d')); 

    return $ary; 
} 

輸出:

Array 
(
    [0] => Array 
     (
      [day] => 2012-12-01 
      [slot] => 1 
     ) 

) 
Array 
(
    [0] => Array 
     (
      [day] => 2012-12-17 
      [slot] => 1 
     ) 

    [1] => Array 
     (
      [day] => 2012-12-18 
      [slot] => 1 
     ) 

) 
1

這似乎來測試確定。 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); 
相關問題