PHP:打印指定日期和月末之间的所有日期


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

I不想返回DATE(Y-m-d)。

我需要打印从给定日期开始直到月末的所有天数,而不依赖于月份或年份

我同时尝试了[$array as$I]和[$arrays as$key],但都没有成功。

$myday(例如=19)
退货$days

将导致:
数组
[0]=>20
[1] =>21
[2] =>22
[3] =>23

[31]=>31||[30]=>30||[28]=>28

我需要$days的每个值来将每个值与另一个字段进行比较。

没有尝试使用$myday作为常规数字,而不是将其视为日期。不要使用strtotime、mktime。。。。

编辑

需要像这样简单的东西:

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

但是print_r不能做到这一点,我需要返回每个值,以便在不同的if条件下使用

这可以使用DateTime()DateInterval()DatePeriod()和相对日期格式轻松完成。

$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');
}

结果

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