我该如何通过Moment PHP打印一个月的几天



我正在使用MOMM.PHP工作。我想打印所有的日子,例如2018年4月:

$weekdayNumbers = [
    1,2,3,4,5,6,7
];
$m = new MomentMoment();
$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(MomentMoment)#4 (6) {
    ["rawDateTimeString":"MomentMoment":private]=>
    string(3) "now"
    ["timezoneString":"MomentMoment":private]=>
    string(3) "UTC"
    ["immutableMode":"MomentMoment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-03-29 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [1]=>
  object(MomentMoment)#5 (6) {
    ["rawDateTimeString":"MomentMoment":private]=>
    string(3) "now"
    ["timezoneString":"MomentMoment":private]=>
    string(3) "UTC"
    ["immutableMode":"MomentMoment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-03-30 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [2]=>
  object(MomentMoment)#7 (6) {
    ["rawDateTimeString":"MomentMoment":private]=>
    string(3) "now"
    ["timezoneString":"MomentMoment":private]=>
    string(3) "UTC"
    ["immutableMode":"MomentMoment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-03-31 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [3]=>
  object(MomentMoment)#8 (6) {
    ["rawDateTimeString":"MomentMoment":private]=>
    string(3) "now"
    ["timezoneString":"MomentMoment":private]=>
    string(3) "UTC"
    ["immutableMode":"MomentMoment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-04-01 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [4]=>
  object(MomentMoment)#9 (6) {
    ["rawDateTimeString":"MomentMoment":private]=>
    string(3) "now"
    ["timezoneString":"MomentMoment":private]=>
    string(3) "UTC"
    ["immutableMode":"MomentMoment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-04-02 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [5]=>
  object(MomentMoment)#10 (6) {
    ["rawDateTimeString":"MomentMoment":private]=>
    string(3) "now"
    ["timezoneString":"MomentMoment":private]=>
    string(3) "UTC"
    ["immutableMode":"MomentMoment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-04-03 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [6]=>
  object(MomentMoment)#11 (6) {
    ["rawDateTimeString":"MomentMoment":private]=>
    string(3) "now"
    ["timezoneString":"MomentMoment":private]=>
    string(3) "UTC"
    ["immutableMode":"MomentMoment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-04-04 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [7]=>
  object(MomentMoment)#12 (6) {
    ["rawDateTimeString":"MomentMoment":private]=>
    string(3) "now"
    ["timezoneString":"MomentMoment":private]=>
    string(3) "UTC"
    ["immutableMode":"MomentMoment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-04-05 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [8]=>
  object(MomentMoment)#13 (6) {
    ["rawDateTimeString":"MomentMoment":private]=>
    string(3) "now"
    ["timezoneString":"MomentMoment":private]=>
    string(3) "UTC"
    ["immutableMode":"MomentMoment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-04-06 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [9]=>
  object(MomentMoment)#14 (6) {
    ["rawDateTimeString":"MomentMoment":private]=>
    string(3) "now"
    ["timezoneString":"MomentMoment":private]=>
    string(3) "UTC"
    ["immutableMode":"MomentMoment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-04-07 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [10]=>
  object(MomentMoment)#15 (6) {
    ["rawDateTimeString":"MomentMoment":private]=>
    string(3) "now"
    ["timezoneString":"MomentMoment":private]=>
    string(3) "UTC"
    ["immutableMode":"MomentMoment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-04-08 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [11]=>
  object(MomentMoment)#16 (6) {
    ["rawDateTimeString":"MomentMoment":private]=>
    string(3) "now"
    ["timezoneString":"MomentMoment":private]=>
    string(3) "UTC"
    ["immutableMode":"MomentMoment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-04-09 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [12]=>
  object(MomentMoment)#17 (6) {
    ["rawDateTimeString":"MomentMoment":private]=>
    string(3) "now"
    ["timezoneString":"MomentMoment":private]=>
    string(3) "UTC"
    ["immutableMode":"MomentMoment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-04-10 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [13]=>
  object(MomentMoment)#18 (6) {
    ["rawDateTimeString":"MomentMoment":private]=>
    string(3) "now"
    ["timezoneString":"MomentMoment":private]=>
    string(3) "UTC"
    ["immutableMode":"MomentMoment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-04-11 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [14]=>
  object(MomentMoment)#19 (6) {
    ["rawDateTimeString":"MomentMoment":private]=>
    string(3) "now"
    ["timezoneString":"MomentMoment":private]=>
    string(3) "UTC"
    ["immutableMode":"MomentMoment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-04-12 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [15]=>
  object(MomentMoment)#20 (6) {
    ["rawDateTimeString":"MomentMoment":private]=>
    string(3) "now"
    ["timezoneString":"MomentMoment":private]=>
    string(3) "UTC"
    ["immutableMode":"MomentMoment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-04-13 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [16]=>
  object(MomentMoment)#21 (6) {
    ["rawDateTimeString":"MomentMoment":private]=>
    string(3) "now"
    ["timezoneString":"MomentMoment":private]=>
    string(3) "UTC"
    ["immutableMode":"MomentMoment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-04-14 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  [17]=>
  object(MomentMoment)#22 (6) {
    ["rawDateTimeString":"MomentMoment":private]=>
    string(3) "now"
    ["timezoneString":"MomentMoment":private]=>
    string(3) "UTC"
    ["immutableMode":"MomentMoment":private]=>
    bool(false)
    ["date"]=>
    string(26) "2018-04-15 11:07:28.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
}

属性date无法直接从对象访问,但是在var_dump()之后(请参阅var_dump之后的php dateTime dateTime dateTime Interval isset更改(。

对象Moment扩展了DateTime类,因此您可以使用format()方法:

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

据我在MONK.PHP的描述中看到的,您会从getMomentsByWeekdays中获得moment objects数组。所以我认为正确的方法是

$value->getDay()

而不是

$value->date;

最新更新