php获取本周周一、周日时间,上周周一、周日时间,本月第一天,本月最后一天,上个月第一天,最后一天时间

来源:互联网 发布:淘宝药品货到付款 8.1 编辑:程序博客网 时间:2024/05/21 08:56
[php] view plain copy
 
 print?
  1. //这个星期的星期一  
  2. // @$timestamp ,某个星期的某一个时间戳,默认为当前时间  
  3. // @is_return_timestamp ,是否返回时间戳,否则返回时间格式  
  4. function this_monday($timestamp=0,$is_return_timestamp=true){  
  5.     static $cache ;  
  6.     $id = $timestamp.$is_return_timestamp;  
  7.     if(!isset($cache[$id])){  
  8.         if(!$timestamp) $timestamp = time();  
  9.         $monday_date = date('Y-m-d', $timestamp-86400*date('w',$timestamp)+(date('w',$timestamp)>0?86400:-/*6*86400*/518400));  
  10.         if($is_return_timestamp){  
  11.             $cache[$id] = strtotime($monday_date);  
  12.         }else{  
  13.             $cache[$id] = $monday_date;  
  14.         }  
  15.     }  
  16.     return $cache[$id];  
  17.   
  18. }  
  19.   
  20. //这个星期的星期天  
  21. // @$timestamp ,某个星期的某一个时间戳,默认为当前时间  
  22. // @is_return_timestamp ,是否返回时间戳,否则返回时间格式  
  23. function this_sunday($timestamp=0,$is_return_timestamp=true){  
  24.     static $cache ;  
  25.     $id = $timestamp.$is_return_timestamp;  
  26.     if(!isset($cache[$id])){  
  27.         if(!$timestamp) $timestamp = time();  
  28.         $sunday = this_monday($timestamp) + /*6*86400*/518400;  
  29.         if($is_return_timestamp){  
  30.             $cache[$id] = $sunday;  
  31.         }else{  
  32.             $cache[$id] = date('Y-m-d',$sunday);  
  33.         }  
  34.     }  
  35.     return $cache[$id];  
  36. }  
  37.   
  38. //上周一  
  39. // @$timestamp ,某个星期的某一个时间戳,默认为当前时间  
  40. // @is_return_timestamp ,是否返回时间戳,否则返回时间格式  
  41. function last_monday($timestamp=0,$is_return_timestamp=true){  
  42.     static $cache ;  
  43.     $id = $timestamp.$is_return_timestamp;  
  44.     if(!isset($cache[$id])){  
  45.         if(!$timestamp) $timestamp = time();  
  46.         $thismonday = this_monday($timestamp) - /*7*86400*/604800;  
  47.         if($is_return_timestamp){  
  48.             $cache[$id] = $thismonday;  
  49.         }else{  
  50.             $cache[$id] = date('Y-m-d',$thismonday);  
  51.         }  
  52.     }  
  53.     return $cache[$id];  
  54. }  
  55.   
  56. //上个星期天  
  57. // @$timestamp ,某个星期的某一个时间戳,默认为当前时间  
  58. // @is_return_timestamp ,是否返回时间戳,否则返回时间格式  
  59. function last_sunday($timestamp=0,$is_return_timestamp=true){  
  60.     static $cache ;  
  61.     $id = $timestamp.$is_return_timestamp;  
  62.     if(!isset($cache[$id])){  
  63.         if(!$timestamp) $timestamp = time();  
  64.         $thissunday = this_sunday($timestamp) - /*7*86400*/604800;  
  65.         if($is_return_timestamp){  
  66.             $cache[$id] = $thissunday;  
  67.         }else{  
  68.             $cache[$id] = date('Y-m-d',$thissunday);  
  69.         }  
  70.     }  
  71.     return $cache[$id];  
  72.   
  73. }  
  74.   
  75. //这个月的第一天  
  76. // @$timestamp ,某个月的某一个时间戳,默认为当前时间  
  77. // @is_return_timestamp ,是否返回时间戳,否则返回时间格式  
  78.   
  79. function month_firstday($timestamp = 0, $is_return_timestamp=true){  
  80.     static $cache ;  
  81.     $id = $timestamp.$is_return_timestamp;  
  82.     if(!isset($cache[$id])){  
  83.         if(!$timestamp) $timestamp = time();  
  84.         $firstday = date('Y-m-d', mktime(0,0,0,date('m',$timestamp),1,date('Y',$timestamp)));  
  85.         if($is_return_timestamp){  
  86.             $cache[$id] = strtotime($firstday);  
  87.         }else{  
  88.             $cache[$id] = $firstday;  
  89.         }  
  90.     }  
  91.     return $cache[$id];  
  92. }  
  93.   
  94. //这个月的第一天  
  95. // @$timestamp ,某个月的某一个时间戳,默认为当前时间  
  96. // @is_return_timestamp ,是否返回时间戳,否则返回时间格式  
  97.   
  98. function month_lastday($timestamp = 0, $is_return_timestamp=true){  
  99.     static $cache ;  
  100.     $id = $timestamp.$is_return_timestamp;  
  101.     if(!isset($cache[$id])){  
  102.         if(!$timestamp) $timestamp = time();  
  103.         $lastday = date('Y-m-d', mktime(0,0,0,date('m',$timestamp),date('t',$timestamp),date('Y',$timestamp)));  
  104.         if($is_return_timestamp){  
  105.             $cache[$id] = strtotime($lastday);  
  106.         }else{  
  107.             $cache[$id] = $lastday;  
  108.         }  
  109.     }  
  110.     return $cache[$id];  
  111. }  
  112.   
  113. //上个月的第一天  
  114. // @$timestamp ,某个月的某一个时间戳,默认为当前时间  
  115. // @is_return_timestamp ,是否返回时间戳,否则返回时间格式  
  116.   
  117. function lastmonth_firstday($timestamp = 0, $is_return_timestamp=true){  
  118.     static $cache ;  
  119.     $id = $timestamp.$is_return_timestamp;  
  120.     if(!isset($cache[$id])){  
  121.         if(!$timestamp) $timestamp = time();  
  122.         $firstday = date('Y-m-d', mktime(0,0,0,date('m',$timestamp)-1,1,date('Y',$timestamp)));  
  123.         if($is_return_timestamp){  
  124.             $cache[$id] = strtotime($firstday);  
  125.         }else{  
  126.             $cache[$id] = $firstday;  
  127.         }  
  128.     }  
  129.     return $cache[$id];  
  130. }  
  131.   
  132. //上个月的第一天  
  133. // @$timestamp ,某个月的某一个时间戳,默认为当前时间  
  134. // @is_return_timestamp ,是否返回时间戳,否则返回时间格式  
  135.   
  136. function lastmonth_lastday($timestamp = 0, $is_return_timestamp=true){  
  137.     static $cache ;  
  138.     $id = $timestamp.$is_return_timestamp;  
  139.     if(!isset($cache[$id])){  
  140.         if(!$timestamp) $timestamp = time();  
  141.         $lastday = date('Y-m-d', mktime(0,0,0,date('m',$timestamp)-1, date('t',lastmonth_firstday($timestamp)),date('Y',$timestamp)));  
  142.         if($is_return_timestamp){  
  143.             $cache[$id] = strtotime($lastday);  
  144.         }else{  
  145.             $cache[$id] =  $lastday;  
  146.         }  
  147.     }  
  148.     return $cache[$id];  
  149. }  
  150. echo '本周星期一:'.this_monday(0,false).'';  
  151. echo '本周星期天:'.this_sunday(0,false).'';  
  152. echo '上周星期一:'.last_monday(0,false).'';  
  153. echo '上周星期天:'.last_sunday(0,false).'';  
  154. echo '本月第一天:'.month_firstday(0,false).'';  
  155. echo '本月最后一天:'.month_lastday(0,false).'';  
  156. echo '上月第一天:'.lastmonth_firstday(0,false).'';  
  157. echo '上月最后一天:'.lastmonth_lastday(0,false).'';  
阅读全文
0 0
原创粉丝点击