在開發過程中會遇到這樣一個需求:獲取2018-11-02到2018-11-15之間的日期數組
希望得到如下數組:
Array ( [0] => 2018-11-02 [1] => 2018-11-03 [2] => 2018-11-04 [3] => 2018-11-05 [4] => 2018-11-06 [5] => 2018-11-07 [6] => 2018-11-08 [7] => 2018-11-09 [8] => 2018-11-10 [9] => 2018-11-11 [10] => 2018-11-12 [11] => 2018-11-13 [12] => 2018-11-14 [13] => 2018-11-15 )
思路:
- 想得到兩個日期之間的數組,需要兩個參數,一個起始日期,一個結尾日期
- while循環 循壞條件結尾日期大於起始日期
- 起始日期放入數組中
- 每次循環起始日期+1天
- 起始日期大於結尾日期時退出,即可得出兩個時間段的數組
代碼:
$arr=periodDate('2018-11-02','2018-11-15'); print_r($arr); function periodDate($start_time,$end_time){ $start_time = strtotime($start_time); $end_time = strtotime($end_time); $i=0; while ($start_time<=$end_time){ $arr[$i]=date('Y-m-d',$start_time); $start_time = strtotime('+1 day',$start_time); $i++; } return $arr; }
---------------------
作者:路過火車
來源:CSDN
原文:https://blog.csdn.net/weixin_42362496/article/details/85204596
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!