本文目錄一覽:
如何使用PHP計算上一個月的今天
?php
$time = time();
/**
* 計算上一個月的今天,如果上個月沒有今天,則返回上一個月的最後一天
* @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);
php如何求上一個月月初至月末?
由於php內置時間函數 strtotime 在求上個月這個功能上存在bug,所以放棄不用了……
上個自己寫的臨時用的,樓主看看:
$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));
echo ‘lastStartDay = ‘.$lastStartDay;
echo ‘br/’;
echo ‘lastEndDay = ‘.$lastEndDay;
php中使用mktime() 如何獲取上一月昨天的時間,今天的時間,明天的時間;
如果一個月固定30天,那真的很好辦,直接當前 時間戳-30*86400 就是上一月今天的時間戳了,加減一次86400就是加減一天。
如果今天幾號要對應上一月幾號,我就提一些注意點吧,當前月份減1和加1當然就是上一個月和下一個月,不過注意要12月和1月的判斷,還有如果今天3月30號,上一個月也沒30號,這些還要看你自己想怎麼處理。只要拿到正確的日期,傳入mktime就拿到時間了,至於昨天和明天,一樣加減一次86400就行了。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/242286.html