當前月
<?php $thismonth = date('m'); $thisyear = date('Y'); $startDay = $thisyear . '-' . $thismonth . '-1'; $endDay = $thisyear . '-' . $thismonth . '-' . date('t', strtotime($startDay)); $b_time = strtotime($startDay); $e_time = strtotime($endDay);
上一月
<?php $thismonth = date('m'); $thisyear = date('Y'); if ($thismonth == 1) { $lastmonth = 12; $lastyear = $thisyear - 1; } else { $lastmonth = $thismonth - 1; $lastyear = $thisyear; } $lastStartDay = $lastyear . '-' . $lastmonth . '-1'; $lastEndDay = $lastyear . '-' . $lastmonth . '-' . date('t', strtotime($lastStartDay)); $b_time = strtotime($lastStartDay); $e_time = strtotime($lastEndDay);
這里對關鍵的就是date函數中的t,它是用來獲取當前月所含天數的,28天,29天,30天,31天。含有多少天,月底就是多少號。