菜单

PHP由周取日期

2012年01月5日 - php

根据指定年份和周数,获取这一周的周一(开始日期)和周日(结束日期)

".getKWBYFY('15/16')."";

function getKWBYFY($fy){
	//FY 10/11 = 01.07.2010 - 30.06.2011
	$fyArray = explode("/", $fy);
	$startYear = '20'.reset($fyArray);
	$endYear = '20'.end($fyArray);
	$output = '';
	$lastWeek = date('W', strtotime($startYear.'-12-31'))>1?date('W', strtotime($startYear.'-12-31')):date('W', strtotime($startYear.'-12-24'));
	for($i=date("W", strtotime($startYear.'-07-01')); $i<$lastWeek+1; $i++){
		$output .= '';
	}
	for($j=1; $j'.$endYear.' // KW '.$j.' '.getWeekDate($endYear, $j).'';
	}
	return $output;
}

function getWeekDate($year,$weeknum){
	$firstdayofyear=mktime(0,0,0,1,1,$year);  
	$firstweekday=date('N',$firstdayofyear);  
	$firstweenum=date('W',$firstdayofyear);  
	if($firstweenum==1){  
		$day=(1-($firstweekday-1))+7*($weeknum-1);  
		$startdate=date('d.m',mktime(0,0,0,1,$day,$year));  
		$enddate=date('d.m',mktime(0,0,0,1,$day+6,$year));  
	}else{  
		$day=(9-$firstweekday)+7*($weeknum-1);  
		$startdate=date('d.m',mktime(0,0,0,1,$day,$year));  
		$enddate=date('d.m',mktime(0,0,0,1,$day+6,$year));  
	}
	return '('.$startdate.' - '.$enddate.')';      
}

?>

示例:

/*
     * year, 1->54, 1234567
     */
    function getWeekDay($year,$weeknum,$week){
        $firstdayofyear=mktime(0,0,0,1,1,$year);
        $firstweekday=date('N',$firstdayofyear);
        $firstweenum=date('W',$firstdayofyear);
        if($firstweenum==1){
            $day=(1-($firstweekday-1))+7*($weeknum-1);
        }else{
            $day=(9-$firstweekday)+7*($weeknum-1);
        }
        return mktime(0,0,0,1,$day+$week-1,$year);
    }


    /*
     * Always from last Friday (00:00:01) to current Thursday (23:59:59) e.g. for this week: 08.11.- 14.11.
     */
    function getLastFridayToCurrentThursdayDate(){
        $lastFriday = $this->getWeekDay(date('Y', time()-86400*7), date('W', time()-86400*7), 5);
        $currentThursday = $this->getWeekDay(date('Y', time()), date('W', time()), 4);
        return array($lastFriday, $currentThursday+86399);
    }

发表评论

电子邮件地址不会被公开。 必填项已用*标注