首頁  >  文章  >  php教程  >  php中已知年份和周数求该周的初始日期与结束日期

php中已知年份和周数求该周的初始日期与结束日期

WBOY
WBOY原創
2016-06-06 19:49:321561瀏覽

如果我已经知道年份和该年的第几周(一年有52周),求这个周的初始日期和结束日期。代码如下: 1 function GetWeekDate( $year , $week ) 2 { 3 $months = array ("1"="Jan.","2"="Feb.","3"="Mar.","4"="Apr.","5"="May.","6"="Jun.","7"="Jul.","8"="Aug."

  如果我已经知道年份和该年的第几周(一年有52周),求这个周的初始日期和结束日期。代码如下:

  

<span> 1</span> <span>function</span> GetWeekDate( <span>$year</span>,<span>$week</span><span>)
</span><span> 2</span> <span>{
</span><span> 3</span>     <span>$months</span> = <span>array</span>("1"=>"Jan.","2"=>"Feb.","3"=>"Mar.","4"=>"Apr.","5"=>"May.","6"=>"Jun.","7"=>"Jul.","8"=>"Aug.","9"=>"Sep.","10"=>"Oct.","11"=>"Nov.","12"=>"Dec."<span>);
</span><span> 4</span>     <span>$time</span> = <span>strtotime</span>("1 January <span>$year</span>", <span>time</span><span>());
</span><span> 5</span>     <span>$day</span> = <span>date</span>('w', <span>$time</span><span>);//求1月1号是第1周的哪一天,0表示星期一,6表示星期日
</span><span> 6</span>     <span>$time</span> += ((7*(<span>$week</span>-1))+1-<span>$day</span>)*24*3600<span>;//时间回归到该年第一周的第一天,因为1月1号并不一定是星期一
</span><span> 7</span>     <span>$m1</span> = <span>date</span>('m', <span>$time</span><span>); 
</span><span> 8</span>     <span>$d1</span> = <span>date</span>('d', <span>$time</span><span>);
</span><span> 9</span>     <span>$time</span> += 6*24*3600<span>;//每周的初始时间与结束时间的时间间隔
</span><span>10</span>     <span>$m2</span> = <span>date</span>('m', <span>$time</span><span>);
</span><span>11</span>     <span>$d2</span> = <span>date</span>('d', <span>$time</span><span>);
</span><span>12</span>     <span>return</span>  <span>$year</span>." the week ".<span>$week</span>. 'th('.<span>$months</span>[(int)<span>$m1</span>].<span>$d1</span>.'-'.<span>$months</span>[(int)<span>$m2</span>].<span>$d2</span>.')'<span>;
</span><span>13</span> }

测试结果:

      $year=2014;

      $week=7;//第七周

      echo GetWeekDate($year,$week);//得到的结果是:2014 the week 7th(Feb.10-Feb.16)

  查询日历验证2014年第七周的初始时间(周一)是2014.2.10,结束时间(周日)是2014.2.16。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn