首頁  >  文章  >  後端開發  >  PHP實現取得上月、本月、近15天、近30天的方法

PHP實現取得上月、本月、近15天、近30天的方法

墨辰丷
墨辰丷原創
2018-05-21 09:33:382532瀏覽

這篇文章主要介紹了PHP簡單取得上月、本月、近15天、近30天的方法,結合實例形式分析了PHP透過自訂函數封裝的日期與時間戳轉換相關運算技巧,需要的朋友可以參考下

具體如下:

/**
 * 获取统计时间
 * @param $type
 * 1 上月
 * 2 本月
 * 3 近15天
 * 4 近30天
 * @return array
 */
function getDateInfo($type)
{
  $data = array(
    array(
      'firstday' => date('Ym01', strtotime('-1 month')),
      'lastday' => date('Ymt', strtotime('-1 month')),
    ),
    array(
      'firstday' => date('Ym01', strtotime(date("Y-m-d"))),
      'lastday' => date('Ymd', strtotime((date('Ym01', strtotime(date("Y-m-d")))) . " +1 month -1 day")),
    ),
    array(
      'firstday' => date('Ymd', strtotime("-15 day")),
      'lastday' => date('Ymd', strtotime('-1 day')),
    ),
    array(
      'firstday' => date('Ymd', strtotime("-30 day")),
      'lastday' => date('Ymd', strtotime('-1 day')),
    ),
  );
  return is_null($type) ? $data : $data[$type-1];
}
print_r(getDateInfo(1));//获取上个月第一天与最后一天

#運行結果:

##

Array
(
  [firstday] => 20170601
  [lastday] => 20170630
)

相關推薦:

#php取得時間程式碼小結分享

#PHP取得時間差

php取得時間與星期幾的程式碼 

以上是PHP實現取得上月、本月、近15天、近30天的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn