2014-03-31 3 views
0

첫 시도 "$ last_visit"과 첫 번째 시도가 30 분이 지난 후에 시도 할 수있는 대기 시간을 계산하려고합니다.PHP의 현재 시간이 앞으로의 시간보다 더 크다

<?php 

    function pa($arr) { 
     echo '<pre>'; 
     print_r($arr); 
     echo '< /pre>'; 
    } 

    $lastv="2014-03-31 02:30:00"; 
    $counter['last']= $lastv." ".strtotime($lastv); 
    $counter['next']= date('Y-m-d H:i:s', ((strtotime($lastv))+ (30 * 60)))." ".((strtotime($lastv))+ (50 * 60));    
    $counter['current']= date('Y-m-d h:i:s', time())." ".time(); 
    $counter['wait']=((strtotime($lastv))+ (30 * 60))-time(); 

    pa($counter); 

?> 

결과

Array 
(
    [last] => 2014-03-31 02:30:00 1396233000 
    [next] => 2014-03-31 03:00:00 1396236000 
    [current] => 2014-03-31 02:57:51 1396277871 
    [wait] => -43071 
) 

코드 ...

확실하지만 코드를 반환 현재 시간이 미래 시간보다 클 수 없음 실행 가능한 @http://runnable.com/UzmAZPw7_WxrNqsy/php-time

+0

죄송합니다. 원하는 것을 간단히 설명해 주시겠습니까? –

+0

사용자가 마지막 시도 30 분 후에 만 ​​작업을 수행 할 수있게하려고합니다. 문제는 내가 현재 시간이 다음 시도 시간 (현재 시간 이후 임)보다 커진다는 것입니다. – user1012345

+0

질문은 다음과 같이 묻는 것이 좋습니다. 다음 시도는 첫 번째 시도보다 적어도 30 분 ** 늦을 수 있습니다. 즉 30 분 이상. –

답변

1

나도 몰라하는 시간 당신이 속해 있지만 그것은 나를 위해 잘 작동하고 있습니다. "현재"시간을 대신에 h 대신 표시하면 혼란 스러울 수 있습니다. 따라서 오늘 오후 (유럽 오후)에는 17 대신 5가 표시됩니다.

이 문제를 해결하면 감각 : 아침에

Array 
(
    [last] => 2014-03-31 02:00:00 1396231200 
    [next] => 2014-03-31 03:00:00 1396234200 
    [current] => 2014-03-31 15:43:47 1396280627 
    [wait] => -45827 
) 

3시 여기

+0

예. 이제 감사합니다. akirk – user1012345

0

-45827이의 수를 계산 날짜 시간 객체를 받아 테스트 기능입니다 = 10시간에서 약 12 ​​시간 전 즉, 조금이었다 대기 시간은 초입니다.. 대기 시간을 초과하면 0을 반환합니다.

문자열을 입력으로 허용하도록 변경했습니다. 나는 '현재 시간'이지나도록 허용했기 때문에 단위 테스트를 할 수 있지만 선택 사항이며 지연도 마찬가지입니다.

지금 변경하려면 '지금'값을 변경하십시오.

테스트 코드 : PHP 5.3.18.

<?php // 22765002/php-current-time-greater-than-time 


    // Testing -- set this to test the code... 
    $currentTime = "2014-03-31 04:25:00"; // current time for testing! 

    // Testing -- set this to when the user messed up 
    $failedAttempt = "2014-03-31 03:00:00"; 

    // show stuff 
    $fa = new DateTime($failedAttempt); // as DateTime 
    $ct = new DateTime($currentTime); // as DateTime 

    // stuff to show 
    $counter['whenFailed']  = $fa->format('d/m/Y H:i'); 
    $counter['current']   = $ct->format('d/m/Y H:i'); 
    $counter['wait'] = secondsToWait($failedAttempt, $currentTime); // call the function 

    pa($counter); // display 


    // ----------------- 
    // show right now!!! 
    // ----------------- 

    echo 'Right now <br />'; 
    echo 'Seconds to wait is: ', secondsToWait("2014-03-31 18:50:00"), '<br />'; 
    echo 'Right now <br />'; 
    exit; 

    /** 
    * @param DateTime/string 
    *     Date and Time of the Failed Attempt 
    *     if string then must be sensible :-/ 
    * 
    * @param DateTime/string -- optional 
    *     The Current Time -- default is now() 
    *     if string then must be sensible :-/ 
    * 
    * @param Integer Number of seconds to wait -- default 1800 

    * This will: 
    * be positive for times less than 30 minutes from the start 
    * be zero  at 30 minites from the start 
    * 
    * We want to know how long we have to 'wait' before trying again... 
    * 
    * We want to show the 'wait' as positive or ZERO! 
    * where zero means they can try again... 
    * 
    * @return integer  number of seconds to wait before next attempt 
    */ 
    function secondsToWait($whenFailed, $currentTime = null, $delaySeconds = 1800) 
    { 
     if (is_string($whenFailed)) { 
      $whenFailed = new DateTime($whenFailed); 
     } 


     if (is_null($currentTime)) { 
     $currentTime = new DateTime(); 
     } 
     else if (is_string($currentTime)) { 
      $currentTime = new DateTime($currentTime); 
     } 


     $whenNextAllowed = new DateTime(); 
     $whenNextAllowed->setTimestamp($whenFailed->getTimestamp() + $delaySeconds); 

     $waitSeconds = $whenNextAllowed->getTimestamp() - $currentTime->getTimestamp(); 

     if ($waitSeconds < 0) { 
      $waitSeconds = 0; 
     } 
     return $waitSeconds; 
    } 
// -------------------------------------------------------------------------- 

    // display 
    function pa($arr) 
    { 
     echo '<pre>'; 
     print_r($arr); 
     echo '< /pre>'; 
    } 
관련 문제