簡體   English   中英

PHP:打印給定日期至月底之間的所有日期

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

不想返回日期(年月日)。

我需要獨立於月份或年份,從給定的日期打印整天直到月底。

我嘗試了[$ array as $ i]-[$ array 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
)

演示版

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM