3

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.

3
  • Hi. Try this: php.net/manual/ro/datetime.format.php .echo $ep1->firstAired->date->format('Y-m-d H:i:s'); Commented Feb 7, 2013 at 14:39
  • what did you expect echo would output when passed an object? unless an object has a "magic" __toString method, you will get nothing (and will even throw an error) Commented Feb 7, 2013 at 14:47
  • Yeah, I now it is stupid mistake, but I'm new to coding and PHP. Thank you for the explanation. Commented Feb 7, 2013 at 14:52

2 Answers 2

3

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');
Sign up to request clarification or add additional context in comments.

Comments

1

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:

Yet another PHP feature.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.