2014-03-31 45 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代替H在“当前”的时间,因此在这一天的时间(在欧洲下午)它显示5,而不是17

如果我解决这个问题,结果品牌感:

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时在早晨为约12小时前,即稍下用10小时= -45827

+0

雅现在感谢akirk – user1012345

0

下面是测试功能,它接受的DateTime对象和计算数目的等待的秒数。如果你已经超过了等待时间,它会返回零。

我已经改变它接受字符串作为输入。我允许通过'当前时间',以便您可以进行单元测试,但它是可选的,延迟也是如此。

更改'正确的'值,看看它现在工作。

测试代码: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>'; 
    } 
相关问题