/*PHP获取当前日期是第几周和本周开始日期和本周结束日期*/
//$now = '2018-11-13';周二
public function getNowTimeInfo($now)
{
$str = array();
//$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
$str['year'] = date('Y', strtotime($now));
$first = 1;
//当日在整年中的第几周
$str['week'] = date('W', strtotime($now));
//获取当前周的第几天 周日是 0 周一到周六是 1 - 6
$w = date('w', strtotime($now));
//获取本周开始日期,如果$w是0,则表示周日,减去 6 天
$week_start = date('Y-m-d', strtotime("$now -" . ($w ? $w - $first : 6) . ' days'));
$str['week_start'] = $week_start;
//本周结束日期
$week_end = date('Y-m-d', strtotime("$week_start +6 days"));
$str['week_end'] = $week_end;
return $str;
返回值:
Array
(
[year] => 2018
[week] => 46
[week_start] => 2018-11-12//周一
[week_end] => 2018-11-18//周日
)
----------------------------------------------------------------------------------------------------------------------------
/**
* 获取一年每周的开始日期和结束日期
* @param $year
* @return mixed
*/
public static 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));可以获取当年以前的年份有多少周,如果是当年还没到12-31号$num为1
$num = 52;//一年约52周
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(
$start_date, $end_day);
}
return $week_array;
}
返回值:共52周
但上面这个获取一年每周的日期遇到头一年最后几天和今年的开始几天是第一周时就不好用了(如2019年第一周是2018-12-31至2019-01-06)而我这个上面的第一周是从2019-01-07至2019-01-13
---------------------------------------------------------------------------------------------------------------------------------------------
如果遇到上面的情况要求周数严谨,可以用下面的:
function get_week_all($year)
{
$whole_week = $this->get_big_week($year);//获取最大周数
$str = '';
for ($i = 1; $i <= $whole_week; $i++) {
$str .= $i . ',';
}
$str_arr = explode(',', rtrim($str, ','));//转成数组
$week_list = array();
foreach ($str_arr as $key => $value) {
$week_list[$key]['num'] = $value;
$week = $this->get_year_month_week($year, $value);//一周的日期
$week_list[$key]['detail'] = "第" . $value . "周(" . $week[0] . "至" . $week[6] . ")";
}
return $week_list;
}
function get_big_week($year_now)
{
$big_week=$this->get_date("W",NULL,$year_now.'-12-31');
if($big_week==1){$big_week=$this->get_date("W",NULL,$year_now.'-12-30');}
if($big_week==1){$big_week=$this->get_date("W",NULL,$year_now.'-12-29');}
if($big_week==1){$big_week=$this->get_date("W",NULL,$year_now.'-12-28');}
if($big_week==1){$big_week=$this->get_date("W",NULL,$year_now.'-12-27');}
if($big_week==1){$big_week=$this->get_date("W",NULL,$year_now.'-12-26');}
if($big_week==1){$big_week=$this->get_date("W",NULL,$year_now.'-12-25');}
if($big_week==1){$big_week=$this->get_date("W",NULL,$year_now.'-12-24');}
return $big_week;
}
function get_year_month_week($year, $value)
{
//指定年和周的日期数组
$time=new \DateTime();
$time->setISODate($year_now,$week_now,1);
$result[]=$time->format('Y-m-d');
$time->setISODate($year_now,$week_now,2);
$result[]=$time->format('Y-m-d');
$time->setISODate($year_now,$week_now,3);
$result[]=$time->format('Y-m-d');
$time->setISODate($year_now,$week_now,4);
$result[]=$time->format('Y-m-d');
$time->setISODate($year_now,$week_now,5);
$result[]=$time->format('Y-m-d');
$time->setISODate($year_now,$week_now,6);
$result[]=$time->format('Y-m-d');
$time->setISODate($year_now,$week_now,7);
$result[]=$time->format('Y-m-d');
return $result;
}
得到结果:
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//PHP计算两个日期的差值
$sdate = new DateTime("1949-10-01");
$edate = new DateTime();
$interval = $sdate->diff($edate);
echo "差值 : " . $interval->y . " 年, " . $interval->m." 月, ".$interval->d." 天 ";
---------------------
作者:锅巴胸
来源:CSDN
原文:https://blog.csdn.net/sinat_15955423/article/details/84033680
版权声明:本文为博主原创文章,转载请附上博文链接!