1

I have a date field, 'field_date', from which I need to extract the year. The date format is yyyy-mm-dd, so I figured the following would do it:

$date = array_pop(array_pop(field_get_items($entity_type, $entity, 'field_date')));
$year = explode("-", $date)[0];
$entity_field[0]['value'] = $year;

This gives me Parse error: syntax error, unexpected '[' Am I not getting the date string correctly?

3

Function array dereferencing was added in PHP 5.4, you must be using an older version.

To fix:

$parts = explode("-", $date);
$year = $parts[0];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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