PHP 当前时间大于未来错误中的时间


PHP current time greater than time in future error

我正在尝试计算第一次尝试"$last_visit"和可能的下一次尝试之间的等待时间可能是在第一次尝试的 30 分钟之后。

不确定,但代码返回当前时间大于未来时间...

<?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
)

code@runnable : http://runnable.com/UzmAZPw7_WxrNqsy/php-time

我不知道

你在哪个时区,但它对我来说工作正常。可能让您感到困惑的是,您用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

这是一个测试函数,它接受 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>';
   }