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

0

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

Would you like to know how I can search only by 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 day 03, it should look like this:

 2017-02-03
 2017-05-03

When doing a search to view records made in the year 2017, it should look like this:

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

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

    
asked by anonymous 26.01.2018 / 16:19

1 answer

1

Use: 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];
    
26.01.2018 / 16:33