php自动获取上一个月的起始时间
1、借鉴评论的方法[20170309 edit]
function get_month_start_end($timestamp) { !empty($timestamp) OR $timestamp = time(); $last_month = date('Y-m-01', $timestamp); $last['first'] = strtotime($last_month); $last['end'] = strtotime("$last_month +1 month -1 seconds"); return $last; }
2、old方法
$last_month = date('Y-m', strtotime('last month'));
$last['first'] = $last_month . '-01 00:00:00';
$last['end'] = date(‘Y-m-d H:i:s’, strtotime("$last_month +1 month -1 day +23 hours +59 minutes +59 seconds"));