How to filter only the date of a field that has date and time set?

Asked

Viewed 1,073 times

1

I need to create a select in Mysql to filter the month in a table, but the field where the date is set has also set the time, thus: [2017-02-04 10:00:00]

How do I filter only the date of this field considering only the month and disregarding other values such as day, year and hours?

  • You can cast a cast for date or use the function date()

2 answers

3


SQL - to return only the month

select MONTH(Nome_coluna) from Nome_tabela

To return the date

select cast(Nome_coluna as date) from Nome_tabela

And return the date considering only the month

select cast(Nome_coluna as date) from Nome_tabela WHERE MONTH(Nome_coluna) = num_mes

being num_mes a number of 1 a 12

0

Use strtotime() then passing the format you want it to return. For example date("m",$time) to redeem the month. See:

$dateValue = "2017-02-04 10:00:00";
$time = strtotime($dateValue);
$month = date("m",$time);

See working in the ideone.

Browser other questions tagged

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