如何使用时刻 php 打印一个月中的几天?

How can I print days of a month with moment php?

我正在与 moment.php 合作。我想打印 2018 年 4 月的所有日期:

$weekdayNumbers = [
    1,2,3,4,5,6,7
];

$m = new \Moment\Moment();
$dates = $m->getMomentsByWeekdays($weekdayNumbers, 3);

echo "<pre>";                    
var_dump($dates);
echo "</pre>"; 

 foreach($dates as $key=>$value){
  $date = $value->date;
     echo $date."<br>";
}

打印了日期,但是当我从代码中删除以下行时:

    echo "<pre>";                    
    var_dump($dates);
    echo "</pre>"; 

...那么我的页面是空的。没有打印任何内容。这怎么可能?

var_dump 输出为:

array(18) {
  [0]=>
  object(Moment\Moment)#4 (6) {
    ["rawDateTimeString":"Moment\Moment":private]=>
    string(3) "now"
    ["timezoneString":"Moment\Moment":private]=>
    string(3) "UTC"
    ["immutableMode":"Moment\Moment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-03-29 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [1]=>
  object(Moment\Moment)#5 (6) {
    ["rawDateTimeString":"Moment\Moment":private]=>
    string(3) "now"
    ["timezoneString":"Moment\Moment":private]=>
    string(3) "UTC"
    ["immutableMode":"Moment\Moment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-03-30 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [2]=>
  object(Moment\Moment)#7 (6) {
    ["rawDateTimeString":"Moment\Moment":private]=>
    string(3) "now"
    ["timezoneString":"Moment\Moment":private]=>
    string(3) "UTC"
    ["immutableMode":"Moment\Moment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-03-31 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [3]=>
  object(Moment\Moment)#8 (6) {
    ["rawDateTimeString":"Moment\Moment":private]=>
    string(3) "now"
    ["timezoneString":"Moment\Moment":private]=>
    string(3) "UTC"
    ["immutableMode":"Moment\Moment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-04-01 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [4]=>
  object(Moment\Moment)#9 (6) {
    ["rawDateTimeString":"Moment\Moment":private]=>
    string(3) "now"
    ["timezoneString":"Moment\Moment":private]=>
    string(3) "UTC"
    ["immutableMode":"Moment\Moment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-04-02 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [5]=>
  object(Moment\Moment)#10 (6) {
    ["rawDateTimeString":"Moment\Moment":private]=>
    string(3) "now"
    ["timezoneString":"Moment\Moment":private]=>
    string(3) "UTC"
    ["immutableMode":"Moment\Moment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-04-03 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [6]=>
  object(Moment\Moment)#11 (6) {
    ["rawDateTimeString":"Moment\Moment":private]=>
    string(3) "now"
    ["timezoneString":"Moment\Moment":private]=>
    string(3) "UTC"
    ["immutableMode":"Moment\Moment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-04-04 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [7]=>
  object(Moment\Moment)#12 (6) {
    ["rawDateTimeString":"Moment\Moment":private]=>
    string(3) "now"
    ["timezoneString":"Moment\Moment":private]=>
    string(3) "UTC"
    ["immutableMode":"Moment\Moment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-04-05 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [8]=>
  object(Moment\Moment)#13 (6) {
    ["rawDateTimeString":"Moment\Moment":private]=>
    string(3) "now"
    ["timezoneString":"Moment\Moment":private]=>
    string(3) "UTC"
    ["immutableMode":"Moment\Moment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-04-06 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [9]=>
  object(Moment\Moment)#14 (6) {
    ["rawDateTimeString":"Moment\Moment":private]=>
    string(3) "now"
    ["timezoneString":"Moment\Moment":private]=>
    string(3) "UTC"
    ["immutableMode":"Moment\Moment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-04-07 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [10]=>
  object(Moment\Moment)#15 (6) {
    ["rawDateTimeString":"Moment\Moment":private]=>
    string(3) "now"
    ["timezoneString":"Moment\Moment":private]=>
    string(3) "UTC"
    ["immutableMode":"Moment\Moment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-04-08 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [11]=>
  object(Moment\Moment)#16 (6) {
    ["rawDateTimeString":"Moment\Moment":private]=>
    string(3) "now"
    ["timezoneString":"Moment\Moment":private]=>
    string(3) "UTC"
    ["immutableMode":"Moment\Moment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-04-09 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [12]=>
  object(Moment\Moment)#17 (6) {
    ["rawDateTimeString":"Moment\Moment":private]=>
    string(3) "now"
    ["timezoneString":"Moment\Moment":private]=>
    string(3) "UTC"
    ["immutableMode":"Moment\Moment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-04-10 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [13]=>
  object(Moment\Moment)#18 (6) {
    ["rawDateTimeString":"Moment\Moment":private]=>
    string(3) "now"
    ["timezoneString":"Moment\Moment":private]=>
    string(3) "UTC"
    ["immutableMode":"Moment\Moment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-04-11 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [14]=>
  object(Moment\Moment)#19 (6) {
    ["rawDateTimeString":"Moment\Moment":private]=>
    string(3) "now"
    ["timezoneString":"Moment\Moment":private]=>
    string(3) "UTC"
    ["immutableMode":"Moment\Moment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-04-12 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [15]=>
  object(Moment\Moment)#20 (6) {
    ["rawDateTimeString":"Moment\Moment":private]=>
    string(3) "now"
    ["timezoneString":"Moment\Moment":private]=>
    string(3) "UTC"
    ["immutableMode":"Moment\Moment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-04-13 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [16]=>
  object(Moment\Moment)#21 (6) {
    ["rawDateTimeString":"Moment\Moment":private]=>
    string(3) "now"
    ["timezoneString":"Moment\Moment":private]=>
    string(3) "UTC"
    ["immutableMode":"Moment\Moment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-04-14 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [17]=>
  object(Moment\Moment)#22 (6) {
    ["rawDateTimeString":"Moment\Moment":private]=>
    string(3) "now"
    ["timezoneString":"Moment\Moment":private]=>
    string(3) "UTC"
    ["immutableMode":"Moment\Moment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-04-15 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
}

据我在 moment.php 的描述中看到,您从 getMomentsByWeekdays 中得到了一个 moment objects 的数组。所以我认为正确的方法是

$value->getDay()

而不是

$value->date;

属性 date 不能直接从对象访问,而是在 var_dump() 之后(参见 PHP DateTime DateInterval isset changes after var_dump)。

对象 Moment 扩展了 DateTime class, so you could use the format() 方法:

foreach($dates as $key=>$value){
   $date = $value->format("Y-m-d");
   echo $date."<br>";
}