简体   繁体   English

php - 如果超过24小时,则使用当前时间验证存储在数据库中的时间戳

[英]php - Validate timestamp stored in the database with the current time if passed 24 hours

The title says it all.. I am not sure what is wrong with my code.. But it didn't go well as i want to 标题说明了一切......我不确定我的代码有什么问题..但它并没有像我想的那样顺利

<?php
//test this
$query = odbc_exec($conn, "SELECT * FROM changepass_req WHERE reqid = 1");
$reqdate = odbc_result($query, 'req_date');
$reqdatex = strtotime($reqdate);
$timenow = date("Y-m-d H:i:s");
    $timenowx = strtotime($timenow);

    if($timenowx <= $reqdatex + 86400) {
    //Less than 24 hours
    echo'Go a head, its not a day passed yet';
    }
    else {
    //More than 24 hours
echo'Sorry, this request is expired!';
    }

?>

The req_date that's in the database is in varchar of datatype. 数据库中的req_date是varchar of datatype。 Thanks for the help in advance 我在这里先向您的帮助表示感谢

EDIT : ALSO the req_date data's are in this format (Ymd H:i:s) 编辑:req_date数据也是这种格式(Ymd H:i:s)

EDIT 2 : The return of var_dump($reqdatexx) looked like this in my page : int(1370855859) 编辑2:var_dump($ reqdatexx)的返回在我的页面中看起来像这样: int(1370855859)

EDIT 3 : Here's the concept i want.. If Database time is before OR WITHIN 24 hours of the CURRENT TIME = return true ELSE // if the Database time is BEYOND or PASSED 24 hours of the CURRENT TIME = return false 编辑3:这是我想要的概念..如果数据库时间在CURRENT TIME之前或之后24小时=返回true ELSE //如果数据库时间是BEYOND或PASSED当前时间24小时=返回false

or , if Database time = 2013 6/8 5:02pm AND Current time = 2013 6/9 5:01pm = return TRUE else //Database time = 2013 g/6 5:02pm AND Current time = 2013 6/9 5:03pm = return FALSE 或者,如果数据库时间= 2013 6/8 5:02 pm和当前时间= 2013 6/9 5:01 pm =返回TRUE否则//数据库时间= 2013 g / 6 5:02 pm AND当前时间= 2013 6/9 5: 03pm =返回FALSE

Hi I am not sure what is exactly you wanted, but to me it seems like you are comparing time now to second.$timenow will return current date and time in following format. 嗨,我不确定你想要的是什么,但对我来说,似乎你现在将时间与第二时间进行比较。$ timenow将以下列格式返回当前日期和时间。

mysql> SELECT NOW();
    -> '2007-12-15 23:50:26'
mysql> SELECT NOW() + 0;
    -> 20071215235026.000000

I suggest you do the following. 我建议你做以下事情。

Test if they are in the same date, if they are echo sorry 测试他们是否在同一天,如果他们回应抱歉

If not then Use the function datediff 如果没有,请使用dateiff函数

mysql> SELECT DATEDIFF('2007-12-31 23:59:59','2007-12-30');
    -> 1
mysql> SELECT DATEDIFF('2010-11-30 23:59:59','2010-12-31');
    -> -31

If it is greater than 1 then you have more than 24 hour, if it is 1 then to be safe just strip the time part from both the date using extract and then compare if the current time is greater in that case you will have more than 24 hour. 如果它大于1那么你有超过24小时,如果它是1然后为了安全只是从使用提取的日期剥离时间部分然后比较当前时间是否更大在这种情况下你将有超过24小时。

Example: mysql> SELECT DATEDIFF('2007-12-31 23:59:59','2007-12-30 22:50:50'); 示例:mysql> SELECT DATEDIFF('2007-12-31 23:59:59','2007-12-30 22:50:50'); -> 1 - > 1

In here it return 1 though it is not 24 hour so compare 23:59:59 and 22:50:50 as 22:50:50 is smaller it is not 24 hour but if we have 在这里它返回1虽然它不是24小时所以比较23:59:59和22:50:50因为22:50:50更小它不是24小时但是如果我们有

mysql> SELECT DATEDIFF('2007-12-31 21:59:59','2007-12-30 22:50:50');
    -> 1 

In here compare 21:59:59 and 22:50:50. 在这里比较21:59:59和22:50:50。 As 22:50:50 is greater we have more than 24 hour. 由于22:50:50更大,我们有超过24小时。

Please read this for details http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html 有关详细信息,请阅读此内容http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html

If you want to check, that $reqdatex is inside +-24 hours interval from now, use this condition: 如果你想检查,$ reqdatex从现在开始间隔+ -24小时,请使用以下条件:

<?php

//Mon, 10 Jun 2013 09:17:39 GMT
$reqdatex = 1370855859;
$timenowx = time();

if(($reqdatex <= $timenowx + 86400) and ($reqdatex >= $timenowx - 86400)) {
    //$reqdatex is inside now +- 24 hours interval
    echo'Go ahead';
}
else {
    //not inside the interval
    echo'Sorry';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM