Thursday, 15 May 2014

php - Query to return results WHERE the datetime is under X months -



php - Query to return results WHERE the datetime is under X months -

i working on json api, , 1 of input parameters specify integer input defined this:

1 - homecoming results within 1 month today 2 - homecoming results within 3 months today 3 - homecoming results within 6 months today 4 - homecoming results within 1 year today

so in essence api used this:

http://url.com/index.php?route=api/order/all&search_time=1, homecoming results table x within 1 month today.

my table has datetime field, date issue not problem. know mysql function can retrieve month month(datetime_item), can utilize where month(datetime_item) = 8, not seem relevant usage.

so in mysql pseudo-code, query this:

where datetime within 3 months ago today

try this,

select * table_name datetime_item >= now()-interval 'x' month

date arithmetic can performed using interval + or - operator:

date + interval expr unit date - interval expr unit

reference - documentation date/time functionsdocumentation date/time functions

php mysql datetime

No comments:

Post a Comment