$time = strtotime("2011-03-31"); /** * 计算上一个月的今天,如果上个月没有今天,则返回上一个月的最后一天 * @param type $time * @return type */ function last_month_today($time){ $last_month_time = mktime(date("G", $time), date("i", $time), date("s", $time), date("n", $time), 0, date("Y", $time)); $last_month_t = date("t", $last_month_time); if ($last_month_t < date("j", $time)) { return date("Y-m-t H:i:s", $last_month_time); } return date(date("Y-m", $last_month_time) . "-d", $time); } echo last_month_today($time);
![](https://img2024.cnblogs.com/blog/35695/202403/35695-20240328092537879-1189167464.jpg)