<?php
print_r(get_thirty_days(new \DateTime()));
print_r(get_thirty_days(new \DateTime('2017-01-15'), 40));
print_r(array_reverse(get_thirty_days(new \DateTime('2017-01-15'), 10)));
function get_thirty_days(\DateTime $date_in, int $days = null) {
$days = $days ?? 30;
$date_out = $date_in->add(new \DateInterval(sprintf('P%dD', $days)));
$dates = [];
while ($days) {
$dates[] = $date_out
->sub(new \DateInterval('P1D'))
->format('Ymd')
;
$days--;
}
return $dates;
}
- Output for 8.4.1 - 8.4.13
- Deprecated: get_thirty_days(): Implicitly marking parameter $days as nullable is deprecated, the explicit nullable type must be used instead in /in/5QjEr on line 7
Array
(
[0] => 20180130
[1] => 20180129
[2] => 20180128
[3] => 20180127
[4] => 20180126
[5] => 20180125
[6] => 20180124
[7] => 20180123
[8] => 20180122
[9] => 20180121
[10] => 20180120
[11] => 20180119
[12] => 20180118
[13] => 20180117
[14] => 20180116
[15] => 20180115
[16] => 20180114
[17] => 20180113
[18] => 20180112
[19] => 20180111
[20] => 20180110
[21] => 20180109
[22] => 20180108
[23] => 20180107
[24] => 20180106
[25] => 20180105
[26] => 20180104
[27] => 20180103
[28] => 20180102
[29] => 20180101
)
Array
(
[0] => 20170223
[1] => 20170222
[2] => 20170221
[3] => 20170220
[4] => 20170219
[5] => 20170218
[6] => 20170217
[7] => 20170216
[8] => 20170215
[9] => 20170214
[10] => 20170213
[11] => 20170212
[12] => 20170211
[13] => 20170210
[14] => 20170209
[15] => 20170208
[16] => 20170207
[17] => 20170206
[18] => 20170205
[19] => 20170204
[20] => 20170203
[21] => 20170202
[22] => 20170201
[23] => 20170131
[24] => 20170130
[25] => 20170129
[26] => 20170128
[27] => 20170127
[28] => 20170126
[29] => 20170125
[30] => 20170124
[31] => 20170123
[32] => 20170122
[33] => 20170121
[34] => 20170120
[35] => 20170119
[36] => 20170118
[37] => 20170117
[38] => 20170116
[39] => 20170115
)
Array
(
[0] => 20170115
[1] => 20170116
[2] => 20170117
[3] => 20170118
[4] => 20170119
[5] => 20170120
[6] => 20170121
[7] => 20170122
[8] => 20170123
[9] => 20170124
)
- Output for 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25
- Array
(
[0] => 20180130
[1] => 20180129
[2] => 20180128
[3] => 20180127
[4] => 20180126
[5] => 20180125
[6] => 20180124
[7] => 20180123
[8] => 20180122
[9] => 20180121
[10] => 20180120
[11] => 20180119
[12] => 20180118
[13] => 20180117
[14] => 20180116
[15] => 20180115
[16] => 20180114
[17] => 20180113
[18] => 20180112
[19] => 20180111
[20] => 20180110
[21] => 20180109
[22] => 20180108
[23] => 20180107
[24] => 20180106
[25] => 20180105
[26] => 20180104
[27] => 20180103
[28] => 20180102
[29] => 20180101
)
Array
(
[0] => 20170223
[1] => 20170222
[2] => 20170221
[3] => 20170220
[4] => 20170219
[5] => 20170218
[6] => 20170217
[7] => 20170216
[8] => 20170215
[9] => 20170214
[10] => 20170213
[11] => 20170212
[12] => 20170211
[13] => 20170210
[14] => 20170209
[15] => 20170208
[16] => 20170207
[17] => 20170206
[18] => 20170205
[19] => 20170204
[20] => 20170203
[21] => 20170202
[22] => 20170201
[23] => 20170131
[24] => 20170130
[25] => 20170129
[26] => 20170128
[27] => 20170127
[28] => 20170126
[29] => 20170125
[30] => 20170124
[31] => 20170123
[32] => 20170122
[33] => 20170121
[34] => 20170120
[35] => 20170119
[36] => 20170118
[37] => 20170117
[38] => 20170116
[39] => 20170115
)
Array
(
[0] => 20170115
[1] => 20170116
[2] => 20170117
[3] => 20170118
[4] => 20170119
[5] => 20170120
[6] => 20170121
[7] => 20170122
[8] => 20170123
[9] => 20170124
)
preferences:
94.02 ms | 416 KiB | 5 Q