欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

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

程序员文章站 2024-01-20 09:01:40
...

给定时间戳,算出本周起止时间 无 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);     
}