简体   繁体   中英

PHP: print all days between a given day and end of month

I don't want to return DATE (Ymd).

I need to print all days until end of month from a given day independently of the month or year .

I tried both [$array as $i] - [$array as $key] and didn't work.

$myday (for example = 19)
return $days

would result:
Array
[0] => 20
[1] => 21
[2] => 22
[3] => 23
...
[31] => 31 || [30] => 30 || [28] => 28

I would need each value for $days to compare each to another field.

Didn't try to use $myday as regular number instead of treating as date. And not use strtotime, mktime....

EDITING

Need something very simple like this one:

$output = array();
for ($i=$myday+1;$i<=31 || $i<=30 || $i<=28;$i++) {
$output[] = $i;
}

But print_r won't do it, I need to return as each value to use in different if conditions

This is easily done using DateTime() , DateInterval() , DatePeriod() , and relative date formats .

$start    = (new DateTime())->setDate(date('Y'), date('m'), $myday + 1);
$end      = new DateTime('first day of next month');
$interval = new DateInterval('P1D');
$period   = new DatePeriod($start, $interval, $end);
$days     = array();
foreach($period as $date) {
    $days[] = $date->format('d');
}

Results

Array
(
    [0] => 20
    [1] => 21
    [2] => 22
    [3] => 23
    [4] => 24
    [5] => 25
    [6] => 26
    [7] => 27
    [8] => 28
    [9] => 29
    [10] => 30
    [11] => 31
)

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM