I'm trying to compare two date fields in a php field inside a view
I already have this working so far so good...
The php field looks like this:
$nid = $data->nid; $node = node_load($nid); $start_value = $node->field_tria['und'][0]['value']; $end_value = $node->field_data_de_resoluci_['und'][0]['value']; $timezone = $node->field_tria['und'][0]['timezone']; $start_date = new DateObject($start_value, $timezone); $end_date = new DateObject($end_value, $timezone); $difference = $start_date->difference($end_date, 'days'); return $difference; This returns me the day difference value, but always in positive. The desired result should be something like this:
Day difference:
Example 1 (negative value):
2 days ago (or -2)
Example 2 (positive value):
2 days
It also looks like loading every node first doesn't seems to be the most efficient way to do this... but it works.
Thanks a lot
Here is my final code:
$nid = $data->nid; $node = node_load($nid); $timezone = $node->field_tria['und'][0]['timezone']; $start_value = $node->field_tria['und'][0]['value']; $day = substr($start_value,8,2); $month = substr($start_value,5,2); $year = substr($start_value,0,4); $date = "$year-$month-$day"; $value1 = new DateObject($date, $timezone); $end_value = $node->field_data_de_resoluci_['und'][0]['value']; $day2 = substr($end_value,8,2); $month2 = substr($end_value,5,2); $year2 = substr($end_value,0,4); $date2 = "$year2-$month2-$day2"; $value2 = new DateObject($date2, $timezone); $difference = $value1->difference($value2, 'days', FALSE); return $difference+1;