国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

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

2024-09-04 11:45:30
字體:
來源:轉載
供稿:網友

用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];

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 筠连县| 栖霞市| 麟游县| 九台市| 二连浩特市| 民勤县| 建水县| 宁海县| 镇坪县| 新建县| 平凉市| 隆尧县| 宁波市| 分宜县| 乌拉特前旗| 怀集县| 垫江县| 淮南市| 苗栗县| 台中县| 华安县| 钦州市| 临夏县| 柏乡县| 嵊泗县| 莒南县| 宕昌县| 司法| 临夏县| 涿鹿县| 黄骅市| 嘉鱼县| 富顺县| 临澧县| 东方市| 陈巴尔虎旗| 南川市| 布拖县| 邮箱| 藁城市| 介休市|