
PHP 计算某日是这一年的第几周
在判断某一天是哪一年的第几周的时候,根据采用的国际标准(忘了叫什么名字了),年首或者年末的那几天有可能不属于今年的第一周或者最后一周。
代码如下:
<?php
echo date("oW",strtotime("20141229"))."\n";
echo date("oW",strtotime('20160101'))."\n";
?>
扩展资料
php计算时间段的天数:
$firstday = date("Y-m-d H:i:s",time());//当前日期
$timestamp=strtotime($firstday);//当前日期时间戳
$firstday=date('Y-m-01',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)-1).'-01'));//上个月开始的日期
$lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));//上个月结束的日期
$stimestamp = strtotime($firstday);
$etimestamp = strtotime($lastday);// 计算日期段内有多少天
$days = ($etimestamp-$stimestamp)/86400+1;// 保存每天日期
$date = array();
for($i=0; $i<$days; $i++){
$date[] = date('Y-m-d', $stimestamp+(86400*$i));
}
2017-09-15
/*
系统自带的date('W',time()) 有bug 比如,一年中的第一天如果不是周一的话,返回值是
52或者53。 可以实测一下。就是说, 系统默认认为这一天是去年的第52/53周。
*/
echo get_weeks_num('2017-01-01');
function get_weeks_num($time){
$month = intval(date('m',$time));//当前时间的月份
$fyear = strtotime(date('Y-01-01',$time));//今年第一天时间戳
$fdate = intval(date('N',$fyear));//今年第一天 周几
$sysweek = intval(date('W',$time));//系统时间的第几周
//大于等于52 且 当前月为1时, 返回1
if(($sysweek >= 52 && $month == 1)){
return 1;
}elseif($fdate == 1){
//如果今年的第一天是周一,返回系统时间第几周
return $sysweek;
}else{
//返回系统周+1
return $sysweek + 1;
}
}
<?php
$strTime = '2017-09-14';
$intWeek = ceil(((strtotime($strTime) - strtotime("2017-01-01 00:00:00")))/(7*86400));
var_dump($strTime."是今年的第".$intWeek."周");
?>
error_reporting(0);
echo date('W',strtotime('2018-03-05'));
echo "<hr>";
echo date('W',strtotime("now"));
echo "<hr>";
$start=intval(date('W',strtotime('2018-03-05')));
//echo $start;
//var_dump($start);
$end=intval(date('W',strtotime('now')));
//echo $end;
//var_dump($end);
$week=$end-$start;
echo $week;
var_dump($week);
?>