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!

Alessandro
  • 164
  • 1
  • 2
  • 11

3 Answers3

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;
}
Fikfattt
  • 448
  • 2
  • 12
  • It is still returning the incorrect value. – Alessandro Jan 12 '21 at 05:14
  • maybe because when you get VoteDate its not call only one data, try use `LIMIT 1` after `ORDER BY logid DESC` – Fikfattt Jan 12 '21 at 05:20
  • 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 – Alessandro Jan 12 '21 at 05:51
  • you can tell me the result if you `echo $voteDate->VoteDate` ? – Fikfattt Jan 12 '21 at 05:59
  • By doing `echo $voteDate->VoteDate` this is the output `2021-01-11 19:58:04.277`. This value is stored in the MSSQL. – Alessandro Jan 12 '21 at 06:02
  • maybe the problem is when you call `$voteDate->VoteDate` , if you only input `2021-01-11 19:58:04.277` to my code its work – Fikfattt Jan 12 '21 at 06:07
  • Yeah, that would be a static time. I need to fetch it from db to check the last vote the user did. – Alessandro Jan 12 '21 at 06:10
  • try this https://stackoverflow.com/questions/5456626/php-pdo-returning-single-row – Fikfattt Jan 12 '21 at 06:13
  • That doesn't have anything to do with dates? I am returning the correct date from my db. – Alessandro Jan 12 '21 at 06:15
  • whats the meaning `It is still returning the incorrect value.` ? its always returning `else` condition ? or something else? – Fikfattt Jan 12 '21 at 06:25
  • i dont know your code to get data from database is okay if i try at my local the only problem if i use `$currentTime = date('');` , but if use `$currentTime = date('Y-m-d H:i:s');` its work – Fikfattt Jan 12 '21 at 06:56
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
Martin Cairney
  • 1,714
  • 1
  • 6
  • 18
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.)