Time date function
I need some help with this script, there seams to be an issue with deviding the date by 24 hours in the days since last login function if it has been less then 1 hours it will still log them in they should only be able to have access once in every 24 hours period.
I am ignoring the seconds and the minutes which I know I shouldn't but I'm not to sure on how to implement it..
function currentMysqlTime($dbh) {
$sql = $dbh->prepare("SELECT CURRENT_TIMESTAMP()");
$sql->execute();
$x = $sql->fetchAll();
return $x[0][0];
}
function lastLoginTime($dbh, $id) {
$sql = $dbh->prepare("SELECT * FROM userpoints WHERE uid = ? AND pid = 2 ORDER BY timestamp desc LIMIT 1");
$sql->execute(array($id));
$y = $sql->fetchAll();
return $y[0][3];
}
function mysqlTimeDiff($dbh, $x, $y) {
$sql = $dbh->prepare("SELECT TIMEDIFF(?, ?)");
$sql->execute(array($x, $y));
$z = $sql->fetchAll();
return $z[0][0];
}
function mysqlDateDiff($dbh, $x, $y) {
$sql = $dbh->prepare("SELECT DATEDIFF(?, ?)");
$sql->execute();
$z = $sql->fetchAll();
return $z[0][0];
}
This is the function that I'm trying to solve.
function daysSinceLastLogin($x) {
$parts = explode('开发者_运维知识库:',$x);
if($parts[0] / 24 == 0) {
//THEN IT IS IS LESS THEN A DAY
}
}
return $parts[0] / 24;
}
Your dateDiff
function isn't passing the $x/$y parameters into your prepared statement, which is no doubt causing errors:
$sql->execute()
should be
$sql->execute(array($x, $y)); /// <--missing the array() part
Try doing a full date comparision. Assuming $x is a full date (2011-09-16 12:30:00)
if ($x >= Date("Y-m-d H:i:s", strtotime("-1 day"))) {
// less than a day
}
精彩评论