PHP 获取某年第几周的开始日期和结束日期的实例

yipeiwu_com6年前PHP代码库

实例如下所示:

/** 
 * 获取某年第几周的开始日期和结束日期 
 * @param int $year 
 * @param int $week 第几周; 
 */ 
 public function weekday($year,$week=1){ 
  $year_start = mktime(0,0,0,1,1,$year); 
  $year_end = mktime(0,0,0,12,31,$year); 
  // 判断第一天是否为第一周的开始 
  if (intval(date('W',$year_start))===1){ 
   $start = $year_start;//把第一天做为第一周的开始 
  }else{ 
   $week++; 
   $start = strtotime('+1 monday',$year_start);//把第一个周一作为开始 
  } 
  // 第几周的开始时间 
  if ($week===1){ 
   $weekday['start'] = $start; 
  }else{ 
   $weekday['start'] = strtotime('+'.($week-0).' monday',$start); 
  } 
  // 第几周的结束时间 
  $weekday['end'] = strtotime('+1 sunday',$weekday['start']); 
  if (date('Y',$weekday['end'])!=$year){ 
   $weekday['end'] = $year_end; 
  } 
  return $weekday; 
 } 
 /** 
 * 计算一年有多少周,每周从星期一开始, 
 * 如果最后一天在周四后(包括周四)算完整的一周,否则不计入当年的最后一周 
 * 如果第一天在周四前(包括周四)算完整的一周,否则不计入当年的第一周 
 * @param int $year 
 * return int 
 */ 
 public function week($year){ 
  $year_start = mktime(0,0,0,1,1,$year); 
  $year_end = mktime(0,0,0,12,31,$year); 
  if (intval(date('W',$year_end))===1){ 
   return date('W',strtotime('last week',$year_end)); 
  }else{ 
   return date('W',$year_end); 
  } 
 } 

以上这篇PHP 获取某年第几周的开始日期和结束日期的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【宜配屋www.yipeiwu.com】。

相关文章

php实现的操作excel类详解

本文实例讲述了php实现的操作excel类。分享给大家供大家参考,具体如下: <?php class Excel { static $instance=null;...

PHP小技巧之函数重载

1.可以使用func_get_args()和func_num_args()这两个函数实现函数的重载!! PHP代码: 复制代码 代码如下:function rewrite() {&nbs...

php使用PDO获取结果集的方法

本文实例讲述了php使用PDO获取结果集的方法。分享给大家供大家参考,具体如下: fetch()方法 fetch()方法用于获取结果集的下一行,语法如下: mixed PDOStatem...

PHP中将字符串转化为整数(int) intval() printf() 性能测试

背景、概述   早在Sql注入横行的前几年,字符串转化为整数就已经被列为每个web程序必备的操作了。web程序将get或post来的id、整数等值强制经过转化函数转化为整数,过滤掉危险字...

PHP中其实也可以用方法链

简单示意一下: 复制代码 代码如下: <?php class test { private $_name = ''; public function setName($name)...