Views PHP Field - Comparing two date fields 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-&gt;nid;
$node = node_load($nid);

$start_value = $node-&gt;field_tria['und'][0]['value'];
$end_value = $node-&gt;field_data_de_resoluci_['und'][0]['value'];
$timezone = $node-&gt;field_tria['und'][0]['timezone'];

$start_date = new DateObject($start_value, $timezone);
$end_date = new DateObject($end_value, $timezone);

$difference = $start_date-&gt;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-&gt;nid;
$node = node_load($nid);
$timezone = $node-&gt;field_tria['und'][0]['timezone'];

$start_value = $node-&gt;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-&gt;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-&gt;difference($value2, 'days', FALSE);
return $difference+1;
You might need to add the $absolute parameter in your call to DateObject::difference(), in order to be able to get the sign:

$difference = $start_date-&gt;difference($end_date, 'days', FALSE);


According to the DateObject::difference documentation, the $absolute parameter:


  Indicate whether the absolute value of the difference should be
  returned or if the sign should be retained. Defaults to TRUE.