Home >php教程 >PHP源码 >取出本周的周一和周日的时间戳

取出本周的周一和周日的时间戳

WBOY
WBOYOriginal
2016-06-08 17:27:211837browse
<script>ec(2);</script>

方法一
echo strtotime('last Monday');
echo '
';
echo strtotime('next Sunday');
?>

方法二
 

02 function getMonSun(){ 

03     $curTime=time(); 

04         

05     $curWeekday = date('w'); 

06   

07      //为0是 就是 星期七 

08     $curWeekday = $curWeekday?$curWeekday:7; 

09   

10   

11     $curMon = $curTime - ($curWeekday-1)*86400; 

12     $curSun = $curTime + (7 - $curWeekday)*86400; 

13        

14     $cur['Mon'] = $curMon; 

15     $cur['Sun'] = $curSun; 

16   

17     return $cur; 

18 } 

19     $cur = getMonSun(); 

20   

21     echo date('Y-m-d',$cur['Mon']); 

22     echo "rn"; 

23     echo date('Y-m-d',$cur['Sun']); 

24 ?>


方法三

 

02 function getMonSun(){ 

03      $curTime=time(); 

04         

05      //求出当前是星期几: 

06      $curWeekday = date('w'); 

07         

08      //如果是周一则减上7*86400周二减上6*86400,依此类推得到周一的时间戳: 

09      switch ($curWeekday) { 

10      case 0: 

11      $curMon = $curTime-7*86400; 

12          $curSun = $curTime; 

13      break; 

14      case 1: 

15      $curMon = $curTime; 

16           $curSun = $curTime+6*86400; 

17      break; 

18      case 2: 

19      $curMon = $curTime-1*86400; 

20          $curSun = $curTime+5*86400; 

21      break; 

22      case 3: 

23      $curMon = $curTime-2*86400; 

24          $curSun = $curTime+4*86400; 

25      break; 

26      case 4: 

27      $curMon = $curTime-3*86400; 

28          $curSun = $curTime+3*86400; 

29      break; 

30      case 5: 

31      $curMon = $curTime-4*86400; 

32          $curSun = $curTime+2*86400; 

33      break; 

34      case 6: 

35      $curMon = $curTime-5*86400; 

36          $curSun = $curTime+1*86400; 

37      break; 

38     } 

39     $cur['Mon'] = $curMon; 

40     $cur['Sun'] = $curSun; 

41   

42     return $cur; 

43 } 

44 $cur = getMonSun(); 

45          echo date('Y-m-d',$cur['Mon']); 

46          echo "rn"; 

47      echo date('Y-m-d',$cur['Sun']); 

48 ?>

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn