简体   繁体   English

PHP:打印给定日期至月底之间的所有日期

[英]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. 我尝试了[$ array as $ i]-[$ array as $ key]都没有用。

$myday (for example = 19) $ myday(例如= 19)
return $days 返回$ days

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

I would need each value for $days to compare each to another field. 我需要$ days的每个值才能将其与另一个字段进行比较。

Didn't try to use $myday as regular number instead of treating as date. 不要尝试将$ myday用作常规数字,而不要当作日期。 And not use strtotime, mktime.... 而不使用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 但是print_r不会这样做,我需要返回每个值以在不同的if条件下使用

This is easily done using DateTime() , DateInterval() , DatePeriod() , and relative date formats . 使用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');
}

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 演示版

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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