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;

2 Answers 2


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->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.

  • Thank you so much, that worked and now it shows the right sign! Now it seems to have a granularity problem, because difference between two dates (the same day month and year) returns a 1... II guess it must be the fault of the seconds and minutes difference between them Probably i have to first extract only the values i want to compare, like day, month and year, and then create the DateObject to compare them. Mar 16, 2017 at 9:05

If both fields are on the same entity (the same node in this case) I'd recommend using the Calculated Field module over Views PHP.

The ability to quickly spin up functions in code for computed fields makes these easier to manage (using version control systems) than using Views PHP and also reduces the attack surface of the site (allowing direct input of PHP into the Drupal UI and thus having PHP living in your DB is never a good thing)

Using computed fields - the approach would be largely the same as that being used now - including the advice in the other answer by @dinopmi.

  • Thank you for your reply, now that it seems to work in php views field, i'll move my code into a computed field just the way you suggest Mar 16, 2017 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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