网站地图    收藏   

主页 > php专栏 > php日期 >

PHP获取本周第一天和最后一天 - php日期

来源:自学PHP网    时间:2014-11-30 11:50 作者: 阅读:

[导读] 用PHP获取本周第一天和最后一天,网上有很多方法,但是太麻烦,或者有bug,这是用php自带的DateTime类实现的方法,比较简单: 本周的第一天和最后一天$date=newDateTime();$date-modify( 39;thisweek...

PHP获取本周第一天和最后一天

用PHP获取本周第一天和最后一天,网上有很多方法,但是太麻烦,或者有bug,这是用php自带的DateTime类实现的方法,比较简单:

  1. //本周的第一天和最后一天 
  2. $date=new DateTime(); 
  3. $date->modify('this week'); 
  4. $first_day_of_week=$date->format('Y-m-d'); 
  5. $date->modify('this week +6 days'); 
  6. $end_day_of_week=$date->format('Y-m-d');   

经过测试modity不知道是用做什么了,于时找了另两个例子,代码如下:

  1. //这个星期的星期一 
  2.  
  3. // @$timestamp ,某个星期的某一个时间戳,默认为当前时间 
  4.  
  5. // @is_return_timestamp ,是否返回时间戳,否则返回时间格式 
  6.  
  7. function this_monday($timestamp=0,$is_return_timestamp=true){ 
  8.  
  9. static $cache ; 
  10.  
  11. $id = $timestamp.$is_return_timestamp
  12.  
  13. if(!isset($cache[$id])){ 
  14.  
  15. if(!$timestamp$timestamp = time(); 
  16.  
  17. $monday_date = date('Y-m-d'$timestamp-86400*date('w',$timestamp)+(date('w',$timestamp)>0?86400:-/*6*86400*/518400)); 
  18.  
  19. if($is_return_timestamp){ 
  20.  
  21. $cache[$id] = strtotime($monday_date); 
  22.  
  23. }else{
  24. $cache[$id] = $monday_date
  25. }
  26. }
  27. return $cache[$id];   
  28. //这个星期的星期天   
  29. // @$timestamp ,某个星期的某一个时间戳,默认为当前时间 
  30.  
  31. // @is_return_timestamp ,是否返回时间戳,否则返回时间格式 
  32.  
  33. function this_sunday($timestamp=0,$is_return_timestamp=true){ 
  34.  
  35. static $cache ; 
  36.  
  37. $id = $timestamp.$is_return_timestamp
  38.  
  39. if(!isset($cache[$id])){ 
  40.  
  41. if(!$timestamp$timestamp = time(); 
  42.  
  43. $sunday = this_monday($timestamp) + /*6*86400*/518400; 
  44.  
  45. if($is_return_timestamp){ 
  46.  
  47. $cache[$id] = $sunday
  48.  
  49. }else
  50.  
  51. $cache[$id] = date('Y-m-d',$sunday);
  52. }
  53. }
  54. return $cache[$id];

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论