sql - MySQL year interval that Includes the entire month from the previous year -
i have mysql condition grabs time interval x number of months. typically, set 13 months can compare current month of last year.
'created > date_sub(now(), interval ' . $timeinterval . ' month)' so example last january compared january, i'd include of previous years month. instead of january 20, 2015 january 20, 2016 have january 01, 2015 current date in january year until february 1st.
i'd use date_format make quick , easy, replace "day" part of date constant. subtract number of months...
... t.created > date_format(now(),'%y-%m-01') - interval ? month as demonstration of returned expression, can test using simple select statement:
select now(), date_format(now(),'%y-%m-01') - interval 12 month now() date_format(now(),'%y-%m-01') - interval 12 month ------------------- ------------------------------------------------- 2016-01-27 21:01:02 2015-01-01 followup
are sure want "greater than" comparison, rather "greater or equal to" comparison >= ?
there other approaches generating date value compare to. use date(now()) or curdate() return current date no time component.
and use day() function numeric value of current day, , subtract (minus 1) number of days. example, this:
>= date(now()) - interval day(now())-1 day - interval 12 month that seems messier , more complicated. think it's easier understand stuffing in '-01' day part.
Comments
Post a Comment