最近在做統計報表的時候要用到列出某個 日期範圍 的時間資料,上網找到相關程式,自已小改一下因為我需要包含最後一天的日期。
$dd= date_range('2020-01-01','2020-01-31');
print_r($dd);
function date_range($first, $last)
{
$last=date('Y-m-d', strtotime($last.'+1 day'));
$period = new DatePeriod(
new DateTime($first),
new DateInterval('P1D'),
new DateTime($last)
);
foreach ($period as $date)
$dates[] = 'log_'.$date->format('Y_m_d');
return $dates;
}
顯示結果:
2020-02-01Array ( [0] => log_2020_01_01 [1] => log_2020_01_02 [2] => log_2020_01_03 [3] => log_2020_01_04 [4] => log_2020_01_05 [5] => log_2020_01_06 [6] => log_2020_01_07 [7] => log_2020_01_08 [8] => log_2020_01_09 [9] => log_2020_01_10 [10] => log_2020_01_11 [11] => log_2020_01_12 [12] => log_2020_01_13 [13] => log_2020_01_14 [14] => log_2020_01_15 [15] => log_2020_01_16 [16] => log_2020_01_17 [17] => log_2020_01_18 [18] => log_2020_01_19 [19] => log_2020_01_20 [20] => log_2020_01_21 [21] => log_2020_01_22 [22] => log_2020_01_23 [23] => log_2020_01_24 [24] => log_2020_01_25 [25] => log_2020_01_26 [26] => log_2020_01_27 [27] => log_2020_01_28 [28] => log_2020_01_29 [29] => log_2020_01_30 [30] => log_2020_01_31 )
參考網址:https://blog.longwin.com.tw/2014/06/php-date-range-list-2014/