我试图计算第一次尝试"$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
)代码@runnable:WxrNqsy/php-time
发布于 2014-03-31 15:47:20
我不知道你在哪个时区,但对我来说很好。让您困惑的是,您用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小时。
发布于 2014-03-31 17:35:07
下面是一个测试函数,它接受DateTime对象并计算等待的秒数。如果超过等待时间,则返回零。
我已经将它更改为接受字符串作为输入。我允许通过“当前时间”,以便您可以对其进行单元测试,但它是可选的,延迟也是如此。
更改“立即”值,以确保它现在起作用。
测试代码:PHP5.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>';
}https://stackoverflow.com/questions/22765002
复制相似问题