0

Suppose I have a timestamp base field on an entity type. I can do $entity->timefield->value and get the UNIX timestamp.

But how do I get a DateTime object from this?

I can see there is a Timestamp::getDateTime() method which does exactly what I want. But that's the DataType class.

The field definition, TimestampItem, defines the 'value' property as a 'timestamp', so that is the Timestamp datatype. But I can't figure out how to get from one to the other.

1
  • You can instantiate a Datetime object with a Unix timestamp.
    – 4uk4
    Nov 30, 2022 at 14:28

1 Answer 1

0

Instantiate a DrupalDateTime object with new DrupalDateTime::createFromTimestamp.

2
  • Is there not a more elegant way? How can I leverage Timestamp::getDateTime()? More generally, given a field item, how do you get the typed data item for one of its properties?
    – joachim
    Dec 1, 2022 at 15:03
  • 1
    Use the method get(), see drupal.stackexchange.com/questions/305408/…
    – 4uk4
    Dec 2, 2022 at 13:45

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.