Getting an express "date" attribute

I have an express object. One of my attributes is a date picker (its handle is testimonial_date).

If i grab that using the code below I get the date i set which is 1st oct 2017

$clientTestimonial->getTestimonialDate()

object(DateTime)#5704 (3) {
  ["date"]=>
  string(26) "2017-10-01 00:00:00.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(13) "Europe/London"
}

but if i try and grab the “date” value

  $clientDate = $clientTestimonal->getTestimonialDate()->date;

I get

 object(DateTime)#5683 (3) {
  ["date"]=>
  string(26) "2023-10-03 16:56:06.336257"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(13) "Europe/London"
}

Why don’t I get the date from the picker?

The date key can’t be accessed like that despite the fact that var_dump gets it.

There are several undocumented ways to access it but they might all stop working when/if PHP patches them.

The only fullproof way to do what you want is to do

$clientDate = $clientTestimonal->getTestimonialDate()->format('Y-m-d H:i:s');

It’s silly because you’re formatting an already formatted date but that’s the “proper” way.

Thank you so much no wonder i was getting stuck

1 Like

dates in PHP are hateful little… things…