简体   繁体   English

检查时间是否超过24小时并显示

[英]Check if the time is more than 24h and show it

I have in my MSSQL database a column with datatype of datetime which contains some dates in this format 2021-01-11 19:58:04.277 .我在我的 MSSQL 数据库中有一个数据类型为datetime的列,其中包含一些格式2021-01-11 19:58:04.277的日期。

This is a voting system, the idea is that the users can only vote once every 24 hours.这是一个投票系统,其理念是用户每 24 小时只能投票一次。

Every time they vote this table is updated with a new record and a new date is added with the corresponding user.每次他们投票时,该表都会更新一条新记录,并为相应的用户添加一个新日期。

I want to display a message that says how many hours left to place the next vote.我想显示一条消息,说明距离下一次投票还有多少小时。

This is the code I am trying to use:这是我尝试使用的代码:

 /**
 * Get Votes Time
 * 
 */
public function getVoteRemainingTime($account) {
    date_default_timezone_get();
    $currentTime = date('Y-m-d H:i:s');

    $sql = "SELECT VoteDate FROM dbo.vote WHERE Account = :account ORDER BY logid DESC";
    $query = $this->db->prepare($sql);
    $query->execute(array(':account' => $account)); 
    $voteDate = $query->fetch(PDO::FETCH_OBJ);

    $timeLeftVote = strtotime($currentTime) - strtotime($voteDate->VoteDate);

    if($timeLeftVote > 86400) {
        return '<strong>Vote Available!</strong>';
    } else {
        return $timeLeftVote;
    }

}

But it is displaying the wrong information.但它显示错误的信息。 What I am doing wrong?我做错了什么? I would appreciate your help.我会很感激你的帮助。

Thanks!谢谢!

you need declare format parameter of the date() like date('Ymd H:i:s')您需要声明date()的格式参数,例如date('Ymd H:i:s')

date_default_timezone_get();
$currentTime = date('Y-m-d H:i:s');

$timeLeftVote = strtotime($currentTime) - strtotime('2021-01-11 19:58:04.277');
if($timeLeftVote > 86400){
    echo 'Vote available';
}else{
    echo $timeLeftVote;
}

Instead of SELECT VoteDate FROM dbo.vote而不是SELECT VoteDate FROM dbo.vote

Can you do the calculation on the time difference at source in the database using您可以使用在数据库中源的时间差进行计算吗

SELECT VoteDate, DATEDIFF(HOUR, VoteDate, GETDATE()) as HourDifference from dbo.vote

As I cannot check your database query, I only checked the rest of the code and it seems to work (as Fikri F mentioned in the comments of this post) if I replace $voteDate->VoteDate by a static date.由于我无法检查您的数据库查询,因此我只检查了代码的 rest,如果我将$voteDate->VoteDate替换为 static 日期,它似乎可以工作(正如本文评论中提到的 Fikri F)。

So please provide more information.所以请提供更多信息。 You could output the current time and the previous vote time from the database as strings, and for both dates as well the result of strtotime, and in the end the result of the method.您可以 output 将数据库中的当前时间和上一个投票时间作为字符串,以及两个日期以及 strtotime 的结果,最后是方法的结果。 Then please explain, what the wrong behaviour is.然后请解释一下,错误的行为是什么。 By this, we can narrow down the problem either to the DB query or to the PHP code.这样,我们可以将问题缩小到 DB 查询或 PHP 代码。 (I would write this as a comment, but I have not enough reputation.) (我会写这个作为评论,但我没有足够的声誉。)

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

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