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

yipeiwu_com5年前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】。

相关文章

详解:——如何将图片储存在数据库里

如果你想把二进制的数据,比如说图片文件和HTML文件,直接保存在你的MySQL数据库,那么这篇文章就是为你而写的!我将告诉你怎样通过HTML表单来储存这些文件,怎样访问和使用这些文件。...

php上传图片之时间戳命名(保存路径)

html代码: <div id="images" style="width:250px;height:120px;background:#fff;border:1px soli...

php 获取一个月第一天与最后一天的代码

复制代码 代码如下:function getthemonth($date) { $firstday = date('Y-m-01', strtotime($date)); $lastda...

php下利用curl判断远程文件是否存在的实现代码

复制代码 代码如下: //判断远程文件 function check_remote_file_exists($url) { $curl = curl_init($url); // 不取回...

php设计模式 Command(命令模式)

复制代码 代码如下: <?php /** * 命令模式 * * 将一个请求封装为一个对象从而使你可用不同的请求对客户进行参数化,对请求排除或记录请求日志,以及支持可取消的操作 */...