2

datetime object is

[start_time] => Array
                (
                    [0] => Array
                        (
                            [value] => Drupal\Core\Datetime\DrupalDateTime Object
                                (
                                    [formatTranslationCache:protected] => 
                                    [inputTimeRaw:protected] => 
                                    [inputTimeAdjusted:protected] => 
                                    [inputTimeZoneRaw:protected] => 
                                    [inputTimeZoneAdjusted:protected] => 
                                    [inputFormatRaw:protected] => 
                                    [inputFormatAdjusted:protected] => 
                                    [langcode:protected] => en
                                    [errors:protected] => Array
                                        (
                                        )

                                    [dateTimeObject:protected] => DateTime Object
                                        (
                                            [date] => 2017-03-17 02:04:03.000000
                                            [timezone_type] => 3
                                            [timezone] => UTC
                                        )

                                    [stringTranslation:protected] => 
                                )

How to get [date] => 2017-03-17 02:04:03.000000 ??????

I have tried using print_r($ruleContentNew[0]['start_time'][0]['value']->getValue());die; But it is giving me error.

  • 1
    where did you try this? clear your question, before down vote make your question clear, your question is not clear, – Yusef Mar 29 '17 at 6:16
  • Threatening with downvotes, soooo constructive! – mchar Mar 29 '17 at 6:25
  • Seems to me that you're using some contrib or custom module based on your start_time machine name. – No Sssweat Mar 29 '17 at 7:06
  • This question doesn't giv4e the necessary information. For example, where do you get the array you are showing, for which entity? Which hook or other special function are you using? – kiamlaluno Mar 29 '17 at 7:19
  • As it is, it's a plain PHP question: dateTimeObject is a protected property, so you cannot access its date property. We cannot say much more, since it's not even clear what you are trying to access. – kiamlaluno Mar 29 '17 at 7:23
7

Try

$node->get('start_time')->getValue();

This actually gives you an array, if you inspect/dump the array, then you'll see that you can get the value like this.

$node->get('start_time')->getValue()[0]['value'];

Alternatively, you can simply use $node->start_time->value;

Note: I am using the default Date field from Core and assuming you're using a Node. Hence the $node, and also worth noting, usually your date machine name will be field_something. Which makes me believe that OPer is using some contrib or custom module.

If you're using some contrib module that allows you to have start and end times on your date field, probably will require a diff solution.

  • yeah, your answer is complete. vote up. – Yusef Mar 29 '17 at 7:14
2

Try

$node->start_time->value;

or if it's multi value

$values = $node->get('start_time')->getValue();
  • not working........It is an array not object. – Sugandh Khanna Mar 29 '17 at 5:50

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