>  기사  >  백엔드 개발  >  지난달, 이번달, 지난 15일, 지난 30일을 구하는 PHP 방식

지난달, 이번달, 지난 15일, 지난 30일을 구하는 PHP 방식

墨辰丷
墨辰丷원래의
2018-05-21 09:33:382614검색

이 글에서는 지난 달, 이번 달, 지난 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는 요일과 함께 시간코드를 얻습니다

위 내용은 지난달, 이번달, 지난 15일, 지난 30일을 구하는 PHP 방식의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.