PHP echo doesn't work as expected

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 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 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've been clear enough and I'm sorry if this is a stupid question. Thank you for your time.

+3


source to share


2 answers


A property $ep1->firstAired

is an object DateTime

. You cannot access these properties directly like you are trying. You have to use accessor methods likeformat()



   echo $ep1->firstAired->format('Y-m-d H:i:s');

      

+3


source


It's actually really, really weird, at least to me. I tested to first display the date property like you did and it didn't work, then I decided to add print_r to see the properties ... And then I was able to print 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 fine, however I remove the line print_r

, it doesn't work.



My system is Windows 8 with php 5.3 running in a shell and here's my proof:

Yet another PHP feature.

So you can say that you can do it, you just need to do print_r first and it probably won't work on all systems.: D

+1


source







All Articles