Home >Backend Development >PHP Tutorial >PHP method to obtain the previous month, this month, the last 15 days, and the last 30 days

PHP method to obtain the previous month, this month, the last 15 days, and the last 30 days

墨辰丷
墨辰丷Original
2018-05-21 09:33:382698browse

This article mainly introduces the method of PHP to simply obtain the last month, this month, the last 15 days, and the last 30 days. It also analyzes the operation skills related to date and timestamp conversion encapsulated by PHP through custom functions in the form of examples. It requires Friends can refer to

for details as follows:

/**
 * 获取统计时间
 * @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));//获取上个月第一天与最后一天

Running results:

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

Related recommendations:

php acquisition timeCode summary sharing

PHP Get Time Difference

# #phpGet time and day of the week code

# #

The above is the detailed content of PHP method to obtain the previous month, this month, the last 15 days, and the last 30 days. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn