Search code examples
phpdatetimevar-dumpgetid3

Object property not showing


I have a function on a php page that is intended to grab the creation date/time of video files using getid3. I thought everything had been working fine and was ready to put this version to bed when I was (of course) running everything one more time to make sure and ended up with an issue I just can't seem to figure out.

The relevant bit of code is this:

$getID3 = new getID3;
$ThisFileInfo = $getID3->analyze($file);
if ($ThisFileInfo["quicktime"]["moov"]["subatoms"][0]["creation_time_unix"]){
    $createdate = new DateTime("@".strval($ThisFileInfo["quicktime"]["moov"]["subatoms"][0]["creation_time_unix"]));
}
else {
    $createdate = new DateTime("@".strval($ThisFileInfo["quicktime"]["moov"]["subatoms"][0]["modify_time_unix"]));
}
$createdate->setTimeZone(new DateTimeZone('America/New_York'));
//var_dump($createdate);
$createdate = $createdate->date;

When I process a file using this portion I get an error stating Notice: Undefined property: DateTime::$date in … line 179(file path removed by me). However, I know that this isn't the case because if I uncomment the var_dump line listed above I get the output object(DateTime)#3 (3) { ["date"]=> string(26) "2016-01-24 15:20:32.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "America/New_York" }.

It certainly looks to me like the DateTime object $createdate has a property called $date, so I'm not sure what's going on, though I'm assuming it's something in my syntax. Can someone help me to sort this out?


Solution

  • If you're not sure which variables are accessible from your current scope of an object, you can use get_class_vars().

    Using it on a DateTime object returns an empty array:

    var_dump(get_class_vars(get_class($datetimeobj)));
    

    ...which means the $date property is private one. Of course, you can access the string representation of the DateTime object with the date_format() method:

    $dateString = date_format($dateTimeObj, 'Y-m-d H:i:s');
    

    You can read more on the format syntax in the "date" manual.