1

I want to alter a field 'Date' in the view to display only year using 'hook_views_query_alter', so that I can display only Year in the date field.

I'm trying below code, which is not working. Can anybody help me?

I'm not sure about assigning new value to the field in the code.

function alb_jobs_views_query_alter(&$view, &$query) {

  if ($view->name == 'test-archive') {
  $query->where[0]['conditions'][0] == array('field'=>"field_data_field_issuu_date.field_issuu_date_value","value"=>'%YEAR(STR_TO_DATE(field_data_field_issuu_date.field_issuu_date_value,"%Y"))%',"operator"=>"LIKE");

  }
}

Here is my date field screenprint:

enter image description here

2

If you are asking for the how to get year from date and use the year in view query alter then

You can use strtotime();

$year = date('Y', strtotime($field_data_field_issuu_date.field_issuu_date_value));

Another way in view you can use custom date format on display and show only year and use that value.(Depends on requirement)

  • thank you for your answer. May i know how to update views date field, with $year – rks Dec 17 '15 at 6:13
  • Im not finding custom date format option.This is a view of type:content. I'm attaching the sceenshot in my question – rks Dec 17 '15 at 6:31
  • thank you, i edited the date format in 'admin/config/regional/date-time/formats', now im able to find only year. – rks Dec 17 '15 at 6:53

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.