国产探花免费观看_亚洲丰满少妇自慰呻吟_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];

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 深泽县| 奎屯市| 陕西省| 香格里拉县| 平泉县| 渝中区| 五指山市| 张掖市| 海安县| 大渡口区| 宣威市| 吴堡县| 钟山县| 文登市| 日照市| 青铜峡市| 原平市| 乌鲁木齐市| 车致| 湛江市| 晋州市| 贵溪市| 方正县| 乐安县| 门头沟区| 扎赉特旗| 渑池县| 长子县| 新巴尔虎右旗| 贵定县| 桃江县| 涟水县| 思茅市| 沈阳市| 祁连县| 东乡族自治县| 庆城县| 绿春县| 泰安市| 建宁县| 大同市|