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

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

May 25, 2016 pm 05:04 PM

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
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)