Reputation: 11
I have a computed field (field:length_of_stay) for calculate the difference between a starting date (field:date_of_admission) and the current date for content type "Patient".
My Computed Code (PHP) in field settings is:
`$field_date_of_admission = array_pop(field_get_items($entity_type, $entity,
'field_date_of_admission'));
$start_date = new DateObject($field_date_of_admission['value'], '');
$current_date = new DateObject('now', '', 'm/d/Y');
$length_of_stay = $start_date->difference($current_date, 'days');
$entity_field[0]['value'] = $length_of_stay;
}`
Display code is:
`$display_output = $entity_field_item['value'];`
It works although it starts from 0 date.
My problem is that when I want to deactivate a patient the computed field continues to calculate the difference which seems wrong for my application purposes. (Content type Patient has also field:active with acceptable values Yes or No).
Is it possible to use an if statement or something? I don't know PHP actually. Can someone provide some guidance here?
Upvotes: 1
Views: 504
Reputation: 11
A friend gave me this code and works for me, so here it is for anyone who may need something similar. It calculates the length of stay, both for active or inactive patients. Content type: "Patient". Fields: field_length_of_stay (computed field), field_date_of_admission (date), field_date_of_discharge (date), field_active (Boolean 1/Yes,0/No).
$field_date_of_admission = array_pop(field_get_items($entity_type, $entity, 'field_date_of_admission'));
$field_active = field_get_items($entity_type, $entity, 'field_active');
$field_date_of_discharge = array_pop(field_get_items($entity_type, $entity, 'field_date_of_discharge'));
if ($field_active[0]['value'] == '1') {
$ad_date = new DateObject($field_date_of_admission['value'], '');
$current_date = new DateObject('now', '', 'm/d/Y');
$los = $ad_date->difference($current_date, 'days');
$entity_field[0]['value'] = $los;
} else {
$ad_date = new DateObject($field_date_of_admission['value'], '');
$dis_date = new DateObject($field_date_of_discharge['value'], '');
$los = $ad_date->difference($dis_date, 'days');
$entity_field[0]['value'] = $los;
}
Upvotes: 0
Reputation: 159
You need to replace the code with following code and need to double check with field_active
field machine name goes here.
$field_date_of_admission = array_pop(field_get_items($entity_type, $entity, 'field_date_of_admission'));
$active = array_pop(field_get_items($entity_type, $entity, 'field_active'));
if ($active) {
$start_date = new DateObject($field_date_of_admission['value'], '');
$current_date = new DateObject('now', '', 'm/d/Y');
$length_of_stay = $start_date->difference($current_date, 'days');
$entity_field[0]['value'] = $length_of_stay;
} else {
$entity_field[0]['value'] = 0;
}
Hope this will help you out.
Upvotes: 0