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

时间:2014-03-31 14:58:55

标签: php datetime timestamp

我正在尝试计算第一次尝试“$ 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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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>';
   }
相关问题