Jump to content
  • Hello visitors, welcome to the Hacker World Forum!

    Red Team 1949  (formerly CHT Attack and Defense Team) In this rapidly changing Internet era, we maintain our original intention and create the best community to jointly exchange network technologies. You can obtain hacker attack and defense skills and knowledge in the forum, or you can join our Telegram communication group to discuss and communicate in real time. All kinds of advertisements are prohibited in the forum. Please register as a registered user to check our usage and privacy policy. Thank you for your cooperation.

    TheHackerWorld Official

php 获取近7天或者30天日期或者更大日期区间的日期方法(转)

 Share


HACK1949

Recommended Posts

原文:https://blog.csdn.net/sqlquan/article/details/87795918

1、封装

/**
 * 获取一个日期范围内的日期
 * @param int $interval 日期范围
 * @param string $type 取值类型,-:获取之前日期;+:获取之后的日期
 * @return array
 */
protected function getDateInterval(int $interval,string $type) :array
{
    $dateArr = [];
    for ($i = $interval - 1; $i >= 0; $i--) {
        array_push($dateArr, date('Y-m-d', strtotime("{$type}{$i} day")));
    }
    if($type=='+')$dateArr=array_reverse($dateArr);
    return $dateArr;
}

2、调用

$rs = $this->getDateInterval(14,'-');
print_r($rs);
array:14 [
  0 => "2022-07-13"
  1 => "2022-07-14"
  2 => "2022-07-15"
  3 => "2022-07-16"
  4 => "2022-07-17"
  5 => "2022-07-18"
  6 => "2022-07-19"
  7 => "2022-07-20"
  8 => "2022-07-21"
  9 => "2022-07-22"
  10 => "2022-07-23"
  11 => "2022-07-24"
  12 => "2022-07-25"
  13 => "2022-07-26"
]


$rs2 = $this->getDateInterval(14,'+');
print_r($rs2);
array:14 [
  0 => "2022-07-26"
  1 => "2022-07-27"
  2 => "2022-07-28"
  3 => "2022-07-29"
  4 => "2022-07-30"
  5 => "2022-07-31"
  6 => "2022-08-01"
  7 => "2022-08-02"
  8 => "2022-08-03"
  9 => "2022-08-04"
  10 => "2022-08-05"
  11 => "2022-08-06"
  12 => "2022-08-07"
  13 => "2022-08-08"
]
Link to post
Link to comment
Share on other sites

 Share

discussion group

discussion group

    You don't have permission to chat.
    • Recently Browsing   0 members

      • No registered users viewing this page.
    ×
    ×
    • Create New...