php获取当前月月初至月末的时间戳,上个月月初至月末的时间戳
当前月
<?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天。含有多少天,月底就是多少号。