Search in Mysql using day, month or year only with PHP

Asked

Viewed 51 times

0

I created a table, in it a field in the format DATETIME to store the day, month, year and time in which a record is made.

I wonder how I can do to search only using day, month or year ?

Example:

I have 4 records:

 2017-02-03
 2017-02-13
 2017-05-03
 2018-01-04

When doing a search to display the records made on 03, should appear like this:

 2017-02-03
 2017-05-03

When doing a search to display the records made in the year 2017, should appear like this:

 2017-02-03
 2017-02-13
 2017-05-03

The same thing should be done for the month. It is possible to do this with PHP?

  • John, did it work? There’s something else I can help you?

1 answer

1


Utilize: YEAR(), MONTH() e DAY():

SELECT * FROM tabela
WHERE YEAR(data) = '2017'
AND MONTH(data) = '07'
AND DAY(data) IN ('1', '25' , '30')

Direct in PHP with explode:

$data = '2017-01-15';

$arrayData = explode("-", $data);

echo "Ano:     ".$arrayData[0];
echo "<br>Mes: ".$arrayData[1];
echo "<br>Dia: ".$arrayData[2];

Browser other questions tagged

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