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

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

PHP中文网
发布: 2016-05-25 17:03:40
原创
1010 人浏览过

我在网上找了半天,找到的都是不能用的,没辙就自己写了一个。
别跟我讨论是不是最优算法,多几个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;
}
登录后复制
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板