I am working with moment.php. I want to print all days of for example the month april 2018:
$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>";
}
There are dates printed, but when I remove from the code the following line:
echo "<pre>";
var_dump($dates);
echo "</pre>";
...then my page is empty. Nothing is printed. How can this be possible?
var_dump output is:
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"
}
}
The property date
is not accessible from the object directly, but after a var_dump()
(see PHP DateTime DateInterval isset changes after var_dump).
The object Moment
extends DateTime
class, so you could use the format()
method:
foreach($dates as $key=>$value){
$date = $value->format("Y-m-d");
echo $date."<br>";
}