Supposing data
is in format DATE
, DATETIME
or TIMESTAMP
, $condicaoData
should be in format YYYY-MM-DD
.
You can do two things: provide the date in the received format and use the function DATE_FORMAT
Mysql or transform the received date into the format required by Mysql.
To use the function DATE_FORMAT
:
$sql = "
SELECT *
FROM noticias
WHERE data >= DATE_FORMAT('$condicaoData', '%d/%m/%Y')
ORDER BY id DESC";
To transform the received format into Mysql format:
// caso você possa gerar a data no formato Y-m-d
$condicaoData = date('Y-m-d', strtotime("-3 days"));
// ou caso você já tenha a data no formato d/m/Y
$condicaoData = preg_replace(
'/^(\d+)\/(\d+)\/(\d+)$/',
'$3-$2-$1',
date('d/m/Y', $condicaoData));
And then SQL would be:
$sql = "
SELECT *
FROM noticias
WHERE data >= '$condicaoData'
ORDER BY id DESC";
Another tip: consider using Prepared Statements, which is the safest option to protect your database against SQL type Injection attacks. So the query is cached in your database and the only things that change are the parameters - in this case, the dates of your query.
The date field is
date
?– rray
If I understand your problem correctly, there’s my answer, hugs!
– thiagobarradas
Remember to choose the best answer. You ask a lot of questions and those who answer like to get feedback as well. Hug.
– thiagobarradas