Home >Backend Development >PHP Tutorial >php get all dates between start date and end date

php get all dates between start date and end date

WBOY
WBOYOriginal
2016-07-28 08:29:32965browse

Due to project requirements, it is necessary to obtain data within a specified date range and process it on a daily basis.

For example, it is necessary to process the data of each day in the date range from 2016-06-01 to 2016-06-05. First, you need to obtain the date of each day in this date range, and then perform processing in a loop.

<code><span><span><?php</span><span>$date</span> = <span>array</span>(<span>'2016-06-01'</span>,<span>'2016-06-02'</span>,<span>'2016-06-03'</span>,<span>'2016-06-04'</span>,<span>'2016-06-05'</span>);
<span>foreach</span>(<span>$date</span><span>as</span><span>$d</span>){
    <span>// 执行处理</span>
}
<span>?></span></span></code>

If the date range spans a large period (2015-09-01 to 2016-06-30), and there are situations where it spans years and months (leap months), manually creating the date array is too time-consuming and unreasonable.

So I wrote the following method to get the date of each day in the specified date range. The code is as follows:

<code><span><span><span><?php</span>
/**
 * 获取指定日期段内每一天的日期
 *<span> @param</span>  Date  $startdate 开始日期
 *<span> @param</span>  Date  $enddate   结束日期
 *<span> @return</span> Array
 */</span><span><span>function</span><span>getDateFromRange</span><span>(<span>$startdate</span>, <span>$enddate</span>)</span>{</span><span>$stimestamp</span> = strtotime(<span>$startdate</span>);
    <span>$etimestamp</span> = strtotime(<span>$enddate</span>);

    <span>// 计算日期段内有多少天</span><span>$days</span> = (<span>$etimestamp</span>-<span>$stimestamp</span>)/<span>86400</span>+<span>1</span>;

    <span>// 保存每天日期</span><span>$date</span> = <span>array</span>();

    <span>for</span>(<span>$i</span>=<span>0</span>; <span>$i</span><<span>$days</span>; <span>$i</span>++){
        <span>$date</span>[] = date(<span>'Y-m-d'</span>, <span>$stimestamp</span>+(<span>86400</span>*<span>$i</span>));
    }

    <span>return</span><span>$date</span>;
}

<span>// demo</span><span>$date</span> = getDateFromRange(<span>'2016-02-25'</span>,<span>'2016-03-05'</span>);
print_r(<span>$date</span>);

<span>?></span></span></span></code>

Output:

<code><span>Array</span>
(
    [<span>0</span>] => <span>2016</span>-<span>02</span>-<span>25</span>
    [<span>1</span>] => <span>2016</span>-<span>02</span>-<span>26</span>
    [<span>2</span>] => <span>2016</span>-<span>02</span>-<span>27</span>
    [<span>3</span>] => <span>2016</span>-<span>02</span>-<span>28</span>
    [<span>4</span>] => <span>2016</span>-<span>02</span>-<span>29</span>
    [<span>5</span>] => <span>2016</span>-<span>03</span>-<span>01</span>
    [<span>6</span>] => <span>2016</span>-<span>03</span>-<span>02</span>
    [<span>7</span>] => <span>2016</span>-<span>03</span>-<span>03</span>
    [<span>8</span>] => <span>2016</span>-<span>03</span>-<span>04</span>
    [<span>9</span>] => <span>2016</span>-<span>03</span>-<span>05</span>
)</code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces php to get all the dates between the start date and the end date, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

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
Previous article:PHP单元测试phpunitNext article:sphinx 排序