Reputation: 33
I have the following PHP code:
$ep1 = $tvdb->getEpisodeById($cur->id, 'en');
var_dump($ep1);
It returns this:
object(TvDb\Episode)#60 (17) {
["id"]=> int(4490826)
["number"]=> int(12)
["season"]=> int(3)
["directors"]=> array(0) { }
["guestStars"]=> array(0) { }
["writers"]=> array(0) { }
["name"]=> string(11) "Episode 312"
["firstAired"]=> object(DateTime)#57 (3) {
["date"]=> string(19) "2013-04-07 00:00:00"
["timezone_type"]=> int(3)
["timezone"]=> string(12) "Europe/Sofia"
}
["imdbId"]=> string(0) ""
["language"]=> string(2) "en"
["overview"]=> string(0) ""
["rating"]=> string(1) "0"
["ratingCount"]=> int(0)
["lastUpdated"]=> object(DateTime)#3 (3) {
["date"]=> string(19) "2013-01-30 22:15:41"
["timezone_type"]=> int(1)
["timezone"]=> string(6) "+00:00"
}
["seasonId"]=> int(501077)
["serieId"]=> int(161511)
["thumbnail"]=> string(0) ""
}
I want to echo "date" and I write the following:
$ep1 = $tvdb->getEpisodeById($cur->id, 'en');
echo ($ep1->firstAired->date);
And it returns nothing, but when I do this:
$ep1 = $tvdb->getEpisodeById($cur->id, 'en');
var_dump($ep1);
echo ($ep1->firstAired->date);
And there after the dump the date shows:
object(TvDb\Episode)#60 (17) { ["id"]=> int(4490826) ["number"]=> int(12) ["season"]=> int(3) ["directors"]=> array(0) { } ["guestStars"]=> array(0) { } ["writers"]=> array(0) { } ["name"]=> string(11) "Episode 312" ["firstAired"]=> object(DateTime)#57 (3) { ["date"]=> string(19) "2013-04-07 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(12) "Europe/Sofia" } ["imdbId"]=> string(0) "" ["language"]=> string(2) "en" ["overview"]=> string(0) "" ["rating"]=> string(1) "0" ["ratingCount"]=> int(0) ["lastUpdated"]=> object(DateTime)#3 (3) { ["date"]=> string(19) "2013-01-30 22:15:41" ["timezone_type"]=> int(1) ["timezone"]=> string(6) "+00:00" } ["seasonId"]=> int(501077) ["serieId"]=> int(161511) ["thumbnail"]=> string(0) "" } 2013-04-07 00:00:0
I don't have this problem with:
$ep1 = $tvdb->getEpisodeById($cur->id, 'en');
echo ($ep1->name);
I hope I was clear enough and I'm sorry If it is a stupid question. Thank you for your time.
Upvotes: 3
Views: 735
Reputation: 10666
Actually this is really, really weird, at least to me. I tested to echo out just the date property at first, just like you did, and it didn't work, then i decided to add a print_r to see the properties... And then I could print out the date-string.
This is my code:
$d = new DateTime();
print_r($d);
echo $d->timezone . PHP_EOL;
echo $d->date . PHP_EOL;
And doing it like this works just fine, however do I remove the print_r
line it doesn't work.
My system is windows 8 with php 5.3 running in a shell, and here's my proof:
So you could say that you can do this, you just have to do a print_r first, and it probably won't work on all systems. :D
Upvotes: 1
Reputation: 41428
The property $ep1->firstAired
is a DateTime
object. You cannot access it's properties directly like you're trying. You have to use the accessor methods like format()
echo $ep1->firstAired->format('Y-m-d H:i:s');
Upvotes: 3