php 获取本周、本月、本年方法
获取本周(周一至周日)
/**
* 获取本周所有日期
*/
public function get_week($time = '', $format='Y-m-d'){
$time = $time != '' ? $time : time();
//获取当前周几
$week = date('w', $time);
$date = [];
for ($i=1; $i<=7; $i++){
$date[$i] = date($format ,strtotime($i-$week.' days',$time));
}
return $date;
}
获取本月(月初至月尾)
//获取本月所有日期
public function get_month(){
$j = date("t"); //获取当前月份天数
$start_time = strtotime(date('Y-m-01')); //获取本月第一天时间戳
$array = array();
for($i=0;$i<$j;$i++){
$array[] = date('Y-m-d',$start_time+$i*86400); //每隔一天赋值给数组
}
return $array;
}
获取本年(一月至十二月)
//获取今年所有月份
public function get_years(){
$year = date('Y');
$yeararr = [];
$month = [];
for ($i=1; $i <=12 ; $i++) {
$yeararr[$i] = $year.'-'.$i;
}
foreach ($yeararr as $key => $value) {
$timestamp = strtotime( $value );
$start_time = date( 'Y-m', $timestamp );
$month[] = $start_time;
}
return $month;
}
评论