0

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.

This is a voting system, the idea is that the users can only vote once every 24 hours.

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!

1
  • What does ...it is displaying the wrong information ... mean? Commented Jan 12, 2021 at 7:21

3 Answers 3

0

you need declare format parameter of the date() like date('Y-m-d 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;
}
Sign up to request clarification or add additional context in comments.

11 Comments

It is still returning the incorrect value.
maybe because when you get VoteDate its not call only one data, try use LIMIT 1 after ORDER BY logid DESC
I've changed it to: "SELECT TOP 1 VoteDate FROM dbo.vote WHERE Account = :account ORDER BY VoteDate DESC" In order to get the last vote date that was inserted in the db. but still same problem
you can tell me the result if you echo $voteDate->VoteDate ?
By doing echo $voteDate->VoteDate this is the output 2021-01-11 19:58:04.277. This value is stored in the MSSQL.
|
0

Instead of 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

Comments

0

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.

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. 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. (I would write this as a comment, but I have not enough reputation.)

1 Comment

Thanks for your time, I already fixed the problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.