我正在与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"
}
}
答案 0 :(得分:1)
据我在moment.php的描述中看到,你从moment objects
获得了一个getMomentsByWeekdays
数组。所以我认为正确的方法是
$value->getDay()
而不是
$value->date;
答案 1 :(得分:1)
无法直接从对象访问属性date
,但在var_dump()
之后(请参阅PHP DateTime DateInterval isset changes after var_dump)。
对象Moment
扩展了DateTime
类,因此您可以使用format()
方法:
foreach($dates as $key=>$value){
$date = $value->format("Y-m-d");
echo $date."<br>";
}