2012-04-03 4 views
0

저는 현재 기본적인 작업이 무엇인지 고심하고 있습니다. 나는 시작 날짜, 종료 날짜 및 카운트를 가지고있다. 두 번째 날짜 사이에 세 번째 매개 변수 (개수)가 몇 시간으로 감소해야하는지 계산해야합니다. 예 : 매시간 .PHP 날짜 사이의 숫자 감소

그 조금, 나는 우리가 OK를 포함했다고 생각한다. 문제는 지금 온다.

웹 사이트가 일 때 일 때만 계산되기를 원합니다. 이 시간은 배열에 저장되며 0 색인은 열려 있고 1은 닫습니다. 또한 날짜는 동적으로 now으로 업데이트됩니다.

Array 
(
    [mon] => Array 
     (
      [0] => 2012-04-03 9:00 
      [1] => 2012-04-03 21:30 
     ) 

    [tue] => Array 
     (
      [0] => 2012-04-03 9:00 
      [1] => 2012-04-03 21:30 
     ) 

    [wed] => Array 
     (
      [0] => 2012-04-03 9:00 
      [1] => 2012-04-03 21:30 
     ) 

    [thu] => Array 
     (
      [0] => 2012-04-03 9:00 
      [1] => 2012-04-03 21.30 
     ) 

    [fri] => Array 
     (
      [0] => 2012-04-03 9:00 
      [1] => 2012-04-03 19:00 
     ) 

    [sat] => Array 
     (
      [0] => 2012-04-03 9:00 
      [1] => 2012-04-03 18:00 
     ) 

    [sun] => Array 
     (
      [0] => 2012-04-03 10:30 
      [1] => 2012-04-03 19:00 
     ) 

) 

그래서 매 시간마다 카운터가 줄어들고 개장 시간 사이에 있습니다. 그러나 우리가 닫히면 카운터 이 오늘 닫는 시간까지 올라갈 곳을 계산해야합니다.

openTimes에는 areWeOpen이라는 변수가 있습니다.이 변수를 사용하여 현재 열려 있는지 또는 닫혀 있는지 확인할 수 있습니다. 우리는 몇 가지 코드 그러나 항상 작동하지 않는 것 같습니다

function countOffers($start, $end, $deals) { 
    global $openTimes; 

    if(strtotime($end) < time()) return 1; 

    define('ONEHOUR', 1); 

    $totalDays = unixtojd(strtotime($end)) - unixtojd(strtotime($start)); 
    $daysBefore = unixtojd(time()) - unixtojd(strtotime($start)); 
    $daysAfter = unixtojd(strtotime($end)) - unixtojd(time()); 
    $startDay = strtolower(date("D", strtotime(date("Y-m-d", strtotime($start))))); 
    $totalHours = 0; 
    $hoursBefore = 0; 

    /* TOTAL HOURS */ 
    for($i = 0; $i <= $totalDays; $i++) { 
     $dayName = strtolower(date("D", strtotime(date("Y-m-d", strtotime($start)) . " +$i days"))); 
     $day = $openTimes->openDays[$dayName]; 
     if($i === 0) { 
      $startHour = explode(" ", $start); 
      $startHour = str_replace(array(":","3"), array(".","5"), $startHour[1]); 
      $endHour = explode(" ", $day[1]); 
      $endHour = str_replace(array(":","3"), array(".","5"), $endHour[1]); 
      $totalHours += $endHour - $startHour; 
     } else { 
      $tempHour = (strtotime($day[1]) - strtotime($day[0]))/3600; 
      $totalHours += (strtotime($day[1]) - strtotime($day[0]))/3600; 
     } 
    } 
    $perHour = round($deals/$totalHours, 1); 

    $today = 0; 
    if($openTimes->areWeOpen === FALSE && $openTimes->morning === FALSE) { 
     /* HOURS UP TO TODAY */ 
     for($i = 0; $i < $daysBefore; $i++) { 
      $day = $openTimes->openDays[strtolower(date("D", strtotime(date("Y-m-d", strtotime($start)) . " +$i days")))]; 
      $hoursBefore += (strtotime($day[1]) - strtotime($day[0]))/3600; 
     } 
    } elseif (strtotime($start) <= time()) { 
     /* HOURS UP TO YESTERDAY */ 
     for($i = 0; $i < ($daysBefore-1); $i++) { 
      $day = $openTimes->openDays[strtolower(date("D", strtotime(date("Y-m-d", strtotime($start)) . " +$i days")))]; 
      $hoursBefore += (strtotime($day[1]) - strtotime($day[0]))/3600; 
     } 

     if(strstr($start, date("Y-m-d", time()))) { 
      $today = ceil((time() - strtotime($start))/3600) - ONEHOUR; 
     } else { 
      $today = ceil((time() - strtotime($openTimes->openDays[strtolower(date("D", time()))][0]))/3600) - ONEHOUR; 
     } 
    } 
    $alreadyGone = $hoursBefore*$perHour; 

    $dealsLeft = $deals - (($hoursBefore*$perHour) + ($today*$perHour)); 
    if($dealsLeft < 0.5) $dealsLeft = 1; 

    return round($dealsLeft); 
} 

그것은 계산합니다 제대로, 그러나 우리가 닫혀있을 때, 그것은 계속 감소 것이다 투쟁 할 것 같다. 나는 이것을하기위한 더 좋은 방법이 있어야한다는 것을 안다, 나는 단지 그것을 이해할 수 없다. 너무 머리가 아파서 문제가 복잡해집니다.

* 편집 : 개방 시간 동안

(배열에서 제공) 나는 동안 배의 값 x 양을 감소해야합니다 * 좋아, 여기에 내가 달성하기 위해 노력하고있어의 아래 휴식입니다 그 날. 닫힌 경우 마지막 종료 시간까지만 값을 줄이고 싶습니다.

카운터의 시작 날짜, 종료 날짜 및 시작 번호가 제공됩니다. 매일 오전 9시에서 오후 9시 사이에 값이 감소 할 수 있습니다. 그 시간이 지난 경우 또는 현재 닫혀있는 경우 마지막 종료 시간 (어제)까지만 줄이기를 원합니다.

+3

주제에서 벗어나지 만 웹 사이트에 개장 시간이있는 이유는 정말로 흥미 롭습니다. – JJJ

+0

직원이 전화를 걸지 않으므로? 온라인 판매는하지 않습니다. 저희 웹 사이트는 온라인 안내 책자와 같습니다. – James

+1

@James 당신은 좋아할 수 있습니다 ... 귀하의 웹 사이트에 쉽게 명시된 시간에 전화하지 말라 ..? –

답변

1

이 코드는 가장 좋고 효율적인 코드는 아니지만 사용자가 요구하는 코드를 수행한다고 생각합니다.

<?php 
/* test data 
$openTimes->openDays = array(
'mon' => array('2012-03-27 09:00', '2012-03-27 21:30'), 
'tue' => array('2012-03-27 09:00', '2012-03-27 21:30'), 
'wed' => array('2012-03-27 09:00', '2012-03-27 21:30'), 
'thu' => array('2012-03-27 09:00', '2012-03-27 21:30'), 
'fri' => array('2012-03-27 09:00', '2012-03-27 19:00'), 
'sat' => array('2012-03-27 09:00', '2012-03-27 18:00'), 
'sun' => array('2012-03-27 10:30', '2012-03-27 19:00'), 
); 
*/ 

function countOffers($start, $end, $deals, $now='') { 
    $start = new DateTime($start); 
    $end = new DateTime($end); 
    $now = new DateTime($now); 
    if ($now <= $start) { 
     return $deals; 
    } 
    if ($now >= $end) { 
     return 0; 
    } 
    $totalHours = openTimeBetween($start, $end)/60/60; 
    $hoursRemaining = openTimeBetween($now, $end)/60/60; 
    $perHour = $deals/$totalHours; 
    return (int)round($hoursRemaining * $perHour); 
} 

function openTimeBetween($start, $end) { 
    $totalTime = 0; 
    $today = new DateTime($start->format('Y-m-d H:i:s')); 
    while ($today <= $end) { 
     $totalTime += openTimeRemaining($today, $start, $end); 
     // set time to midnight the next day 
     $today->setTime(0, 0, 0); 
     $today->modify('+1 day'); 
    } 
    return $totalTime; 
} 

function openTimeRemaining($current, $minTime, $maxTime) { 
    global $openTimes; 
    // get the open/close times 
    $day = strtolower($current->format('D')); 
    list($open, $close) = $openTimes->openDays[$day]; 
    $open = new DateTime($open); 
    $close = new DateTime($close); 
    // set the date to be the same as $current 
    $open->setDate($current->format('Y'), $current->format('m'), $current->format('d')); 
    $close->setDate($current->format('Y'), $current->format('m'), $current->format('d')); 

    // if it's past closing time or past the maximum time 
    if ($current > $close || $current > $maxTime) { 
     return 0; 
    } 
    // if it's the first day, count from $minTime or $current, whichever is later 
    else if ($current->format('Y-m-d') === $minTime->format('Y-m-d')) { 
     $diff = max($minTime, $current, $open)->diff($close); 
    } 
    // if it's the last day, count to $maxTime or $close, whichever is earlier 
    else if ($current->format('Y-m-d') === $maxTime->format('Y-m-d')) { 
     $diff = max($current, $open)->diff(min($maxTime, $close)); 
    } 
    // otherwise count the total open time 
    else { 
     $diff = $open->diff($close); 
    } 
    return $diff->h * 60 * 60 + $diff->i * 60 + $diff->s; 
} 

는 현재 시간으로 사용할 수있는 네 번째 매개 변수 문자열로 countOffers()를 호출 할 수 있습니다 테스트합니다.

$start = '2012-03-27 11:00:00'; 
$end = '2012-04-08 19:00:00'; 
$offers = 200; 
$now = '2012-04-04 17:00:00'; 
countOffers($start, $end, $offers, $now); 

추가 매개 변수는 현재 시간을 기본값으로 둡니다.

+0

너무 가까이서 느낄 수 있습니다. 그러나 그것이 아침이고 우리가 닫히면 나는 어제 닫는 시간으로 돌아갈 수 없다. 나는 바이올린을 가질거야! 하지만 고마워! – James

+0

모두 정렬 됨! 고맙습니다. 3 – James