close

轉自 http://www.webzone8.com/article/77.html

取得星期或月份的第一天和最後一天的Function

帶入參數給字串, 可用strtotime相容的格式, 如"+1 month"、"-1 week"

--

得到星期的第一天和最后一天:

PHP代码
        
  1.     
  2.  gettheday(){  function$day
  3.     
  4.   = (, ());  $lastdaydate'Y-m-d'strtotime"$day Sunday"
  5.     
  6.   = (, ());  $firstdaydate'Y-m-d'strtotime"$day - 6 days"
  7.     
  8.    //或
  9.     
  10.   = (, ());  $firstdaydate'Y-m-d'strtotime"$lastday - 6 days"
  11.     
  12.   (, );  returnarray$firstday$lastday
  13.     
  14. }  
  15.     
  16.    
  17.     
  18. print_r(gettheday());  '2010-04-06'
  19.     
  20.   //得到上周的第一天与最后一天
  21.     
  22. print_r(gettheday());  "last week Sunday"
  23.     
  24.   //或
  25.     
  26. print_r(gettheday());  "-1 week Sunday"
  27.     
  28. ;  die

 

获得指定日期所在月的第一天和最后一天:

PHP代码
        
  1.  getthemonth(){  function$date
  2.     
  3.   = (, ());  $firstdaydate'Y-m-01'strtotime$date
  4.     
  5.   = (, ());  $lastdaydate'Y-m-d'strtotime"$firstday +1 month -1 day"
  6.     
  7.   (, );  returnarray$firstday$lastday
  8.     
  9. }  
  10.     
  11.    
  12.     
  13. print_r(getthemonth());  "2010-04-06"
  14.     
  15.   //得到当前月的第一天和最后一天
  16.     
  17. print_r(getthemonth());  "now"
  18.     
  19.   //得到上个月的第一天与最后一天
  20.     
  21. print_r(getthemonth());  "-1 month"
  22.     
  23. print_r(getthemonth());  "last month"
  24.     
  25.   //得到下个月的第一天与最后一天
  26.     
  27. print_r(getthemonth());  "+1 month"

--

全站熱搜
創作者介紹
創作者 dizzy03 的頭像
dizzy03

碎碎念

dizzy03 發表在 痞客邦 留言(0) 人氣()