想了解我如何计算两个日期之间的时间。我正在开发解锁用户功能,以下是我所拥有的:
// get offender's username from session
$username = $_SESSION['UserName'];
require_once('./connections/mysql.php');
$checklockoutexpiration = mysqli_query($conn,"SELECT IsLockedOut, LastLockoutDate FROM users WHERE UserName = '$username' Limit 1") or die($dataaccess_error);
if(mysqli_num_rows($checklockoutexpiration) == 1)
{
$row = mysqli_fetch_array($checklockoutexpiration);
$islockedout = $row['IsLockedOut'];
$lastlockoutdate = $row['LastLockoutDate'];
}
if(**$islockedout == 1 && $lastlockoutdate + LOCKOUT_DURATION_IN_MINUTES < getdate()**)
{
// unlock user
}
锁定日期在数据库中的形式为NOW(),看起来像2010-10-13 13:01:05。我假设它是服务器时间,目前是我的本地开发机器。
问题:如何在代码中加粗。
在asp.net中我会做类似的事情:
// if lockout datetime + lockout duration is less than datetime now
if (usrInfo != null && usrInfo.IsLockedOut && usrInfo.LastLockoutDate.ToUniversalTime().AddMinutes(passwordAttemptLockoutDuration) < DateTime.UtcNow)
{
// then automatically Unlock user
usrInfo.UnlockUser();
}
有人可以告诉我这是怎么做的吗? 谢谢!
答案 0 :(得分:3)
使用http://php.net/strtotime将“2010-10-13 13:01:00”格式的日期转换为自纪元以来的整数秒。
使用http://php.net/time将纪元以来的当前秒数作为整数。
然后很容易。
答案 1 :(得分:1)
已经有了可以接受的答案......所以我会展示一种不同的方法。我将您的查询字符串更改为:
SELECT IsLockedOut, LastLockoutDate,
(addtime(LastLockoutDate, $passwordAttemptLockoutDuration) < NOW()) unlocked
FROM users WHERE UserName = '$username' Limit 1
其中$ passwordAttemptLockoutDuration形式为“hours:minutes:seconds”的字符串
“unlocked”值如果仍然被锁定则为0(或为false),如果他们可以再次登录则为1。
答案 2 :(得分:0)
简短回答:将时间戳和锁定时长转换为秒,然后进行比较。
答案很长:
$lockout = ($lockoutDurationMinutes * 60);
$timestamp= strtotime($yourTimeStampFieldVariable);
if ( ($timestamp + $lockout) > time())
{
// still locked out
}
答案 3 :(得分:0)
如果您想更像ASP示例,请使用DateTime类。您应该能够从字符串构造一个,并且与其对应的DateInterval类一起,可以add一段时间间隔或使用diff来获取两次间隔。
(看起来这只适用于PHP 5.3。这可能是一个交易破坏者。)
答案 4 :(得分:0)
感谢您的所有建议。我最后通过添加一个新列(LastUnlockDate)来解决问题。当用户被锁定时,我将IsLockedOut设置为1,将LastLockoutDate设置为NOW(),将LastUnlockDate设置为NOW()+ $ lockoutduration(以秒为单位)。
然后我的部分代码看起来像:
//------------------------------------------------------------
// if lockout session exits
//------------------------------------------------------------
if(!empty($_SESSION['count']))
{
require_once('./connections/mysql.php');
require_once('./web.config.php');
require_once('./controls/login/error_messages.php');
$count = $_SESSION['count'];
$offender = mysqli_real_escape_string($conn, $_SESSION['offender']);
$maxloginattempt = MAX_LOGIN_ATTEMPT;
if($count >= $maxloginattempt)
{
echo $maxloginattempt_error;
$showform = 0;
// check if user exists in database
$checkoffender = mysqli_query($conn, "SELECT UserName, IsLockedOut, LastLockoutDate, LastUnlockDate, NOW() AS ServerTime FROM users WHERE UserName = '$offender' LIMIT 1")
or die($dataaccess_error);
if(mysqli_num_rows($checkoffender) == 1)
{
$row = mysqli_fetch_array($checkoffender);
$islockedout = $row['IsLockedOut'];
$lastlockoutdate = $row['LastLockoutDate'];
$lastunlockdate = $row['LastUnlockDate'];
$servertime = $row['ServerTime'];
}
// unlock user if lockout has expired
if($islockedout == 1 && $lastunlockdate < $servertime)
{
$unlockoffender = mysqli_query($conn,"UPDATE users SET IsLockedOut = 0 WHERE UserName = '$offender'")
or die($updateactivity_error);
unset($_SESSION['count']);
unset($_SESSION['offender']);
$showform == 1;
}
// otherwise lockout the user
if($islockedout == 0)
{
$lockoutduration = (LOCKOUT_DURATION * 60);
$updateoffender = mysqli_query($conn,"UPDATE users SET IsLockedOut = 1, LastLockoutDate = NOW(), LastUnlockDate = (NOW() + $lockoutduration) WHERE UserName = '$offender'")
or die($updateactivity_error);
}
}
}
由于此代码完全依赖于会话的存在,因此如果浏览器关闭且会话丢失,则数据库中的用户仍设置为已锁定。因此,为了解决这个问题,我修改了我的实际登录页面,该页面现在在身份验证期间进行检查,并在锁定已经过期时解锁用户。
谢谢大家的帮助和建议。总结一下,我的问题是,如何为日期添加时间 - 简单答案是:现在()+ $ lockoutduration