方法一:
<php
header("Content-type:text/html;charset=utf-8");
date_default_timezone_set("Asia/Shanghai");
$year = (int)$_GET['year'];
$week = (int)$_GET['week'];
$weeks = date("W", mktime(0, 0, 0, 12, 28, $year));
echo $year '年一共有' $weeks '周<br />';
if ($week > $weeks || $week <= 0)
{
$week = 1;
}
if ($week < 10)
{
$week = '0' $week;
}
$timestamp['start'] = strtotime($year 'W' $week);
$timestamp['end'] = strtotime('+1 week -1 day', $timestamp['start']);
echo $year '年第' $week '周开始时间戳:' $timestamp['start'] '<br />';
echo $year '年第' $week '周结束时间戳:' $timestamp['end'] '<br />';
echo $year '年第' $week '周开始日期:' date("Y-m-d", $timestamp['start']) '<br />';
echo $year '年第' $week '周结束日期:' date("Y-m-d", $timestamp['end']);
>
方法二: <php
header("Content-type:text/html;charset=utf-8");
function getIsoWeeksInYear($year)
{
$date = new DateTime;
$date->setISODate($year, 53);
return ($date->format("W") === "53" 53 : 52);
}
function weekday($custom_date)
{
$week_start = date('d-m-Y', strtotime('this week monday', $custom_date));
$week_end = date('d-m-Y', strtotime('this week sunday', $custom_date));
$week_array[0] = $week_start;
$week_array[1] = $week_end;
return $week_array;
}
echo '<br> Weeks in 2013<br>' getIsoWeeksInYear(2013);
$weekday = weekday(strtotime(date('d-m-Y', strtotime('5-8-2013'))));
echo '<br> 10-8-2013';
echo '<br>Start: ' $weekday[0];
echo '<br>End: ' $weekday[1];
>
或者方法三:
function get_week($year) {
$year_start = $year "-01-01";
$year_end = $year "-12-31";
$startday = strtotime($year_start);
if (intval(date('N', $startday)) != '1') {
$startday = strtotime("next monday", strtotime($year_start)); //获取年第一周的日期
}
$year_mondy = date("Y-m-d", $startday); //获取年第一周的日期
$endday = strtotime($year_end);
if (intval(date('W', $endday)) == '7') {
$endday = strtotime("last sunday", strtotime($year_end));
}
$num = intval(date('W', $endday));
for ($i = 1; $i <= $num; $i++) {
$j = $i -1;
$start_date = date("Y-m-d", strtotime("$year_mondy $j week "));
$end_day = date("Y-m-d", strtotime("$start_date +6 day"));
$week_array[$i] = array (
str_replace("-",
"",
$start_date
), str_replace("-", "", $end_day));
}
return $week_array;
}
函数get_week()通过传入参数$year年份,获取当年第一天和最后一天所在的周数,计算第一周的日期,通过循环获取每一周的第一天和最后一天的日期。最后返回是一个数组。
想得到指定周数的开始日期和结束日期,比如2011年第18周的开始日期和结束日期,代码如下:
复制代码 代码如下:
$weeks = get_week(2011);
echo '第18周开始日期:'$weeks[18][0]'';
echo '第18周结束日期:'$weeks[18][1];
最后输出结果:
第18周开始日期:20110502
第18周结束日期:20110508
<php header("content-type:text/html;charset=utf-8"); date_default_timezone_set('PRC'); function getWeek($unixTime=''){ $unixTime=is_numeric($unixTime)$unixTime:time(); $weekarray=array('日','一','二','三','四','五','六','七'); return "星期"$weekarray[date('w',$unixTime)]; } $var=getWeek(); switch($var){ case '星期天': $var=time(); $var2=$var-(846006); echo "今天是"date("Y-m-d",$var)"号<br/>这个星期一是"date("Y-m-d",$var2)"号"; break; case '星期一': $var=time(); $var2=$var; echo "今天是"date("Y-m-d",$var)"号<br/>这个星期一是"date("Y-m-d",$var2)"号"; break; case '星期二': $var=time(); $var2=$var-84600; echo "今天是"date("Y-m-d",$var)"号<br/>这个星期一是"date("Y-m-d",$var2)"号"; break; case '星期三': $var=time(); $var2=$var-(846002); echo "今天是"date("Y-m-d",$var)"号<br/>这个星期一是"date("Y-m-d",$var2)"号"; break; case '星期四': $var=time(); $var2=$var-(846003); echo "今天是"date("Y-m-d",$var)"号<br/>这个星期一是"date("Y-m-d",$var2)"号"; break; case '星期五': $var=time(); $var2=$var-(846004); echo "今天是"date("Y-m-d",$var)"号<br/>这个星期一是"date("Y-m-d",$var2)"号"; break; case '星期六': $var=time(); $var2=$var-(846005); echo "今天是"date("Y-m-d",$var)"号<br/>这个星期一是"date("Y-m-d",$var2)"号"; break; }>
public function index(){
$data=time();
$format='星期';
$week = date( "D",$data);
switch($week){
case "Mon":$current = $format"一";break;
case "Tue":$current = $format"二";break;
case "Wed":$current = $format"三";break;
case "Thu":$current = $format"四";break;
case "Fri":$current = $format"五";break;
case "Sat":$current = $format"六";break;
case "Sun":$current = $format"日";break;
}
echo '今天是:'$current;echo "
";//获取当前时间是上午还是下午
// 小写g、h表示12小时制,大写G、H表示24小时制。
echo "现在是"date("H",$data)'点'"";
echo "现在是"date("G",$data)'点'"";
if(date("H",$data)<=12){echo "上午";}else{echo "下午";}// return $current;}
以上就是关于PHP计算一年多少个星期和每周的开始和结束日期全部的内容,包括:PHP计算一年多少个星期和每周的开始和结束日期、PHP获取当前日期及本周一是几月几号的方法、PHP获取当天或某个日期是星期几以及当前时间是几点,上午还是下午等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)