本文实例讲述了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);//当前月的月末时间戳
上一月
$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天。含有多少天,月底就是多少号。
ps:本站还提供了一个unix时间戳转换工具,包含了各种常见语言针对时间戳的操作方法,提供给大家参考:
unix时间戳(timestamp)转换工具:
希望本文所述对大家php程序设计有所帮助。
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!