0

I have a global PHP field provided by views php module. I tried two functions to calculate the date difference, date_diff() and $fieldDate->difference() but it always gives me an ajax error. I think it causes by date format of the date field but I don't know how to fix it. What is right here?

$today=date("m/d/Y"); 
$load = $data->field_field_date_loading[0]['raw']['value'];
$diff=date_diff($load,$today);
echo $diff->format("%R%a days");

or

$today=date("m/d/Y"); 
$load = $data->field_field_date_loading[0]['raw']['value'];
$diff = $load->difference($today, 'days');
echo $diff;
4

The function date() is a formatting function, if you want to use date_diff() your code needs to be something like:

$today = new DateTime();
$load = new DateTime($data->field_field_date_loading[0]['raw']['value']);
$diff = date_diff($load, $today);

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.