首頁  >  文章  >  後端開發  >  php 取得上月、本月、近15天、近30天的方法程式碼範例

php 取得上月、本月、近15天、近30天的方法程式碼範例

怪我咯
怪我咯原創
2017-07-10 09:49:472586瀏覽

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

本文實例講述了PHP簡單獲取上月、本月、近15天、近30天方法。分享給大家參考,具體如下:

/**
 * 获取统计时间
 * @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 取得上月、本月、近15天、近30天的方法程式碼範例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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