Home >php教程 >php手册 >给定任意时间算出该周的起止时间

给定任意时间算出该周的起止时间

WBOY
WBOYOriginal
2016-06-06 19:38:151489browse

给定时间戳,算出本周起止时间 无 function getWeekDate($timestamp) {//年份 如2015年$year = date('Y', $timestamp);//本年份第几周 $weeknum = date('W', $timestamp);//年份中的第几天 0 到 365$day = date('z', $timestamp);//周几 1(表示星期一)到 7

给定时间戳,算出本周起止时间
function getWeekDate($timestamp) {
	//年份 如2015年
	$year = date('Y', $timestamp);
	//本年份第几周 
	$weeknum = date('W', $timestamp);
	//年份中的第几天 0 到 365
	$day = date('z', $timestamp);
	//周几 1(表示星期一)到 7(表示星期天) 
	$weekday = date('N', $timestamp);
	//本年的第一天是周几
	$firstDayOfWeek = date('w', mktime(0,0,0,1,1,$year));
	//echo $year.'--'.$weeknum.'--'.$day.'--'.$weekday;
	//本年的第一周
	if ($weeknum == 1) {
		$startdate = date('Y-m-d',mktime(0,0,0,1,1,$year));
		$enddate = date('Y-m-d',mktime(0,0,0,1,7-$firstDayOfWeek+1,$year));
		//最后几天并入下一年第一周的情况,如2014/12/29算2015年第一周 
		//非下一年第一周 (>7表示前一年的最后几天)
		if ($day > 7) {
			//每周的开始时间 $day+1-$weekday+1
			$startdate = date('Y-m-d',mktime(0,0,0,1,$day+1-$weekday+1,$year));
			$enddate = date('Y-m-d',mktime(0,0,0,12,31,$year));
		}
	} elseif ($weeknum == 53) { //本年最后一周
		$startdate = date('Y-m-d',mktime(0,0,0,1,$day+1-$weekday+1,$year));
		$enddate = date('Y-m-d',mktime(0,0,0,12,31,$year));
	}else {//其他情况
		$startdate = date('Y-m-d',mktime(0,0,0,1,$day+1-$weekday+1,$year));
		$enddate = date('Y-m-d',mktime(0,0,0,1,$day+7-$weekday+1,$year));
	}
	return array($startdate,$enddate);     
}
Statement:
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