PHP implements two algorithms for the day before the specified time n months
/*** Return the date specified before $ monthes month according to $ endtime */function severalMonthAgo ($ endtime, $ monthes) {if (! $ Endtime) {return false;} if (! Is_int ($ monthes) | $ monthes <= 0) {return false;} $ m = date ("m", $ endtime); $ y = date ("Y ", $ endtime); $ d = date ("d", $ endtime); $ year = floor ($ monthes/12); $ monthes_mod = $ monthes % 12; if ($ year = 0) {// <12 months if ($ m <$ monthes) {// The Current Month is less than n months ago $ res_y = $ Y-1; $ res_m = 12-($ monthes-$ m); $ t = date ("t", strtotime ($ res_y. "-". $ res_m. "-01"); if ($ d <$ t) {$ res_d = $ d;} else {$ res_d = $ t ;}} elseif ($ m ==$ monthes) {$ res_y = $ Y-1; $ res_m = 12; $ t = date ("t", strtotime ($ res_y. "-". $ res_m. "-01"); if ($ d <$ t) {$ res_d = $ d;} else {$ res_d = $ t ;}} elseif ($ m> $ monthes) {$ res_y = $ y; $ res_m = $ m-$ monthes; $ t = date ("t", strtotime ($ res_y. "-". $ res_m. "-01"); if ($ d <$ t) {$ res_d = $ d;} else {$ res_d = $ t ;}}} elseif ($ year> 0) {$ y = $ y-$ year; $ monthes = $ monthes_mod; if ($ m <$ monthes) {// The Current Month is less than n months ago $ res_y = $ Y-1; $ res_m = 12-($ monthes-$ m); $ t = date ("t ", strtotime ($ res_y. "- ". $ Res_m. "-01"); if ($ d <$ t) {$ res_d = $ d;} else {$ res_d = $ t ;}} elseif ($ m ==$ monthes) {$ res_y = $ Y-1; $ res_m = 12; $ t = date ("t", strtotime ($ res_y. "-". $ res_m. "-01"); if ($ d <$ t) {$ res_d = $ d;} else {$ res_d = $ t ;}} elseif ($ m> $ monthes) {$ res_y = $ y; $ res_m = $ m-$ monthes; $ t = date ("t", strtotime ($ res_y. "-". $ res_m. "-01"); if ($ d <$ t) {$ res_d = $ d;} else {$ res_d = $ t ;}} return strtotime ($ res_y. "-". $ res_m. "-". $ res_d);}/*** according $ Endtime, returns the specified day before $ monthes */function severalmonthagex ($ endtime, $ monthes) {if (! $ Endtime) {return false;} if (! Is_int ($ monthes) | $ monthes <= 0) {return false;} $ m = date ("m", $ endtime); $ y = date ("Y ", $ endtime); $ d = date ("d", $ endtime); $ year = floor ($ monthes/12); $ monthes_mod = $ monthes % 12; $ y-= $ year; // $ year = 0 or> 0; // $ monthes_mod = 0 or> 0 or if ($ year> 0) {if ($ monthes_mod> 0) {$ m_diff = 12 + $ m-$ monthes_mod; $ m_diff_p = floor ($ m_diff/12); $ m_diff_mod = $ m_diff % 12; if ($ m_diff_p> 0) {$ res_y = $ y; $ res_m = $ m_diff_mod;} elseif ($ m_diff_p = 0) {$ res_y = $ y-1; $ res_m = $ m_diff_mod;} elseif ($ monthes_mod = 0) {// the entire division $ res_y = $ y; $ res_m = $ m ;}} elseif ($ year = 0) {$ m_diff = 12 + $ m-$ monthes_mod; $ m_diff_p = floor ($ m_diff/12); $ m_diff_mod = $ m_diff % 12; if ($ m_diff_p> 0) {$ res_y = $ y;} elseif ($ m_diff_p = 0) {$ res_y = $ y-1;} $ res_m = $ m_diff_mod ;} $ t = date ("t", strtotime ($ res_y. "-". $ res_m. "-01"); if ($ d <$ t) {$ res_d = $ d;} else {$ res_d = $ t;} return strtotime ($ res_y. "-". $ res_m. "-". $ res_d );}