Return records from 00 am to date

Asked

Viewed 54 times

-4

I’m trying to get the recorded votes of 00:00 so far and my code shows that there is no.

$query = mysqli_query($mysqli, "SELECT * FROM votos_1 WHERE DATE(date) = DATE(NOW())");
$rowcount=mysqli_num_rows($query);

    if ($rowcount == 0)
    {
        echo "não tem";
    } else {
        echo "tem";
    }

Note: I have already put the function date_default_timezone_set() for America/Fortaleza, as well as for others.

Example of the date format inside the column date: 1577380495.

1 answer

1


You can do this using the BETWEEN or the operators >= and <=. I will leave the 2 examples below.

// 00h do dia atual
$todayMidnight = (new \Datetime('today midnight'))->format('Y-m-d H:i:s');

// Obs.: Substitua o created_at pelo nome da sua coluna
// Exemplo utilizando o BETWEEN
$query = mysqli_query($mysqli, "SELECT * FROM votos_1 WHERE created_at between {$todayMidnight} and curdate()");
$rowCount = mysqli_num_rows($query);

// Exemplo utilizando os operadores <= e >=
$query = mysqli_query($mysqli, "SELECT * FROM votos_1 WHERE created_at >= {$todayMidnight} and created_at <= curdate()");
$rowCount = mysqli_num_rows($query);

if ($rowCount == 0) {
   echo "não tem";
} else {
   echo "tem";
}
  • Even testing both methods, the error "no" still appears. I updated the question and added the date format in the column.

  • The name of your column is date? If it is you should use the crase between the word because date is a reserved Mysql word. Otherwise, you save the timestamp?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.