Trying to get sys.createdAt from a PHP SDK Query

Just like the title suggests. I’m currently trying to get the createdAt field. I can see this is delivered with the entry fields, but it looks like it’s protected. Any advice?

Thanks!

Hi there,

Could you explain a bit more what you refer by the property being protected?

Yes sure. I can the propert says protected in the PHP response. I’m simply looking for a way to gain access to the createdAt field. Is this possible?

Hey there,

you can access the sys.createdAt property from any resource (entry, asset, content type, etc) like this:

$createdAt = $entry->getSystemProperties()->getCreatedAt();

This will return an object of class Contentful\Core\Api\DateTimeImmutable, which extends PHP’s native \DateTimeImmutable in order to add proper support for printing ISO 8601 strings, as used internally in Contentful. Of course, you can use your own $createdAt->format($formatString) or simply cast it as a string to get the ISO 8601 representation.

Overall, anything in the sys property will be accessible using $entry->getSystemProperties(). So if you want to have access to the sys.revision field, you will use $entry->getSystemProperties()->getRevision().

Hope this helps!

Davide