Home > php教程 > PHP源码 > 获得一段时间内所在的所有周的时间

获得一段时间内所在的所有周的时间

PHP中文网
Release: 2016-05-25 17:04:56
Original
1031 people have browsed it

php代码

<?
	/*  作用由起止日期算出其中的周
	 *  @param start_date 开始日期
	 *  @param end_date   结束日期
	 *  @return 一个二维数组,其中一维为每周起止时间
	 *  @author anngly
         *  @date 2013-06-08
         *  注意:end_date>state_date
	 **/
	
        function getWeek($startdate,$enddate)
	{
		//参数不能为空
		if(!empty($startdate) && !empty($enddate)){

			//先把两个日期转为时间戳
			$startdate=strtotime($startdate);
			$enddate=strtotime($enddate);
			//开始日期不能大于结束日期
			if($startdate<=$enddate){
				$end_date=strtotime("next monday",$enddate);
				if(date("w",$startdate)==1){
			      	$start_date=$startdate;
			    }else{
			      	$start_date=strtotime("last monday",$startdate);
			    }
			    //计算时间差多少周
			    $countweek=($end_date-$start_date)/(7*24*3600);
			    for($i=0;$i<$countweek;$i++){
			    	$sd=date("Y-m-d",$start_date);
			    	$ed=strtotime("+ 6 days",$start_date);
			    	$eed=date("Y-m-d",$ed);
			    	$arr[]=array($sd,$eed);
			    	$start_date=strtotime("+ 1 day",$ed);
			    }
			    return $arr;	
			}
		}
	}

	@var_dump(getWeek( "2012-08-04","2013-08-08")) ;
?>
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template