>php教程 >PHP源码 >获取一年内的星期列表及每周起始时间

获取一年内的星期列表及每周起始时间

PHP中文网
PHP中文网원래의
2016-05-25 17:03:40990검색

我在网上找了半天,找到的都是不能用的,没辙就自己写了一个。
别跟我讨论是不是最优算法,多几个CPU时钟周期,死不了。

public static function getWeeks($format="second",$year=null){
if($year == null){
$year=date('Y');
}
$second_of_day = 24*3600;
$year_start = $year ."-01-01 00:00:00"; 
$year_end = $year ."-12-31 23:59:59"; 
$startday = strtotime($year_start); 
$day_of_week = date('N', $startday);
$first_week_start=$startday;
$first_week_end = $startday+(7-$day_of_week)*$second_of_day+$second_of_day-1;
$week_array[0]=array($first_week_start,$first_week_end);

$endday = strtotime($year_end); 
$day_of_week = date('N', $endday);

$last_week_start=$endday-$day_of_week*$second_of_day+1;
$last_week_end = $endday;
$last_week=array($last_week_start,$last_week_end);

$during = $last_week_start - $first_week_end;

$during_total_week = intval($during/(7*$second_of_day));

for($i=0;$i<$during_total_week;$i++){
$week_array[$i+1]=array($first_week_end+(7*$i*$second_of_day)+1,$first_week_end+(($i+1)*$second_of_day*7));
}

$week_array[$during_total_week+1]=$last_week;

if($format !="second"){
foreach($week_array as $i => $week){
$week_array_of_date_format[$i]=array(date($format,$week[0]),date($format,$week[1]));
}
return $week_array_of_date_format;
}

return $week_array;
}
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.