I'm trying to get the total number of days in a date range field using Views PHP. Any help will be greatly appreciated.

I tried the code below first in "value code" then in "output code" but it didn't do anything.

$start = strtotime($row->field_start_and_end_date:value);
$stop = strtotime($row->field_start_and_end_date:value2); 
$days = $stop-$start;
echo $days;
  • Implementing a functionality or a layout seen in a site, for which just a description of the feature, a screenshot, or a site URL is provided is considered off-topic. At least show what you already have, so we can help you with improving that – Neograph734 Jan 8 '17 at 16:18
  • If I am not mistaken, Views PHP should not echo the value, but return it. Try return $days instead. :) – Neograph734 Jan 8 '17 at 16:45
  • If all of @Neograph734 suggestions fail, try putting your start and stop values through the PHP strip_tags function $start = strip_tags(strtotime($row->field_start_and_end_date:value)); and $stop = strip_tags(strtotime($row->field_start_and_end_date:value2)); – No Sssweat Jan 8 '17 at 18:10
  • I changed the code after adding the date field twice and selecting start date for the first and end date for the second. This is my code $start = strip_tags(strtotime($row->field_start_and_end_date)); $stop = strip_tags(strtotime($row->field_start_and_end_date_1))‌; $days = $stop-$start; return $days;. But I'm getting the error message "Parse error: syntax error, unexpected : runtime-created function on line 2 (T_STRING)" and "Fatal error: Function name must be a string". – Dee Jan 8 '17 at 19:30
  • What are you trying to get with $row->field_start_and_end_date:value and why do you think it should work? That is not the correct PHP syntax. – kiamlaluno Jan 12 '17 at 10:49

Within Views PHP, there are 2 fields. The value code field and the output code field. The first is used to calculate the value (for filtering, sorting, etc). The second is used to display the value.

So in the first field you'll have to perform your calculations and return the final value. If your field is numeric, try to only calculate the number here. Prefixes and suffixes can be added in the output field. (So the value for this field could be 3.)

In the output field you again have to return the final value. This can be the same value as calculated in the value field (there is a token for this value). But you can also append pre- and suffixes and return a string. (Here you can for instance append days to the value to have it display 3 days.)

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.