2

I am trying to call a custom date field to use in a text pane from a node to use as YYYY-mm-dd in:

<div class="swod-date swod-hide">yyyy-mm-dd</div>

I have tried to use:

<?php echo date('Y-m-d', strtotime($node->field_custom_date['und'][0]['value']));?>

but this is not working. I have tied to just print the php but get a result of 1970-01-01 instead of the date in the field.

What am I missing?

1
  • 1
    Are you sure $node->field_custom_date['und'][0]['value'] returns timestamp
    – Vamsi
    Oct 4, 2016 at 4:14

2 Answers 2

2

Check format_date() function. We can also pass timezone from date field (if any). For example on how to use this function check Formatting the way the date/time is displayed.

$custom_date = strtotime($node->field_custom_date['und'][0]['value']);
echo format_date($custom_date, 'custom', 'D j M Y');
2
  • I tried this but it is still nothing is displaying. I am putting the code in a text pane in panelizer using the php filter. When I just put echo date("Y-m-d"); I can get the current date to display. Oct 4, 2016 at 5:28
  • Did you tried checking the actual output of $node->field_custom_date['und'][0]['value'] field in node object, whether it is a Timestamp or Human Readable date format. That will help us print date properly.
    – Yogesh
    Nov 19, 2016 at 17:28
1

Please try with below code.

echo date('Y-m-d', strtotime($node->field_custom_date[LANGUAGE_NONE][0]['value']))
1
  • Please edit your answer to explain what this code is doing, that will reduce the risk that this will be perceived as a "low quality answer", and deleted via moderation. Just a suggestion ... Nov 18, 2016 at 17:04

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.