PHP時間日期處理整理
//返回一個時間段內所有月份 傳時間戳function monthList($start,$end){ if(!is_numeric($start)||!is_numeric($end)||($end<=$start)) return ''; $start=date('Y-m',$start); $end=date('Y-m',$end); //轉為時間戳 $start=strtotime($start.'-01'); $end=strtotime($end.'-01'); $i=0; $d=array(); while($start<=$end){ //這里累加每個月的的總秒數 計算公式:上一月1號的時間戳秒數減去當前月的時間戳秒數 $d[$i]=trim(date('Y-m',$start),' '); $start+=strtotime('+1 month',$start)-$start; $i++; } return $d;
}
//返回一個時間段內周的開始和結束日期 傳date類型
function monthList($start,$end){ if(!is_numeric($start)||!is_numeric($end)||($end<=$start)) return ''; $start=date('Y-m',$start); $end=date('Y-m',$end); //轉為時間戳 $start=strtotime($start.'-01'); $end=strtotime($end.'-01'); $i=0; $d=array(); while($start<=$end){ //這里累加每個月的的總秒數 計算公式:上一月1號的時間戳秒數減去當前月的時間戳秒數 $d[$i]=trim(date('Y-m',$start),' '); $start+=strtotime('+1 month',$start)-$start; $i++; } return $d; }
//返回一個月份的第一天和最后一天
function getthemonth($date)
{
$firstday = date('Y-m-01', strtotime($date));
$lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
return array($firstday,$lastday);
}
$today = date("Y-m-d");
$day=getthemonth($today); </pre>