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

怪我咯
Release: 2023-03-13 10:24:01
Original
2630 people have browsed it

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 combines the example form to analyze the date and ## encapsulated by PHP through the custom function #Timestamp Conversion related operation skills, friends in need can refer to the following

The example of this article describes the simple method of obtaining the previous month, this month, the last 15 days, and the last 30 days in PHP. Share it with everyone for your reference, the details are 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));//获取上个月第一天与最后一天
Copy after login

Running results:

Array
(
  [firstday] => 20170601
  [lastday] => 20170630
)
Copy after login

The above is the detailed content of PHP method code example to obtain the last 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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!