Skip to content

Instantly share code, notes, and snippets.

@rafaehlers
Last active November 14, 2017 18:04
Show Gist options
  • Save rafaehlers/973c0beee6b95606a9640bf3c45ec929 to your computer and use it in GitHub Desktop.
Save rafaehlers/973c0beee6b95606a9640bf3c45ec929 to your computer and use it in GitHub Desktop.
Calculate days between two date fields
<?php
add_shortcode( 'gv_days', 'gv_calculate_days' );
function gv_calculate_days( $atts ) {
global $gravityview_view;
extract($gravityview_view->field_data); // create a $entry variable with current entry data array
extract( shortcode_atts(
array(
'start_date_id' => '',
'end_date_id' => '',
), $atts )
);
$start_date = $entry[$start_date_id];
$end_date = $entry[$end_date_id];
if (empty($start_date) || empty($end_date)) {
return 'Error: No field ID specified.';
}else{
$start = new DateTime($start_date);
$end = new DateTime($end_date);
$diff = $start->diff($end)->days;
}
return $diff;
}
//example usage inside a Custom Content field: [gv_days start_date_id="2" end_date_id="3"]
@offordscott
Copy link

I used this for around a month and it worked just fine. Now though, it is just displaying the short code/calculation instead of the content. Any ideas?

@rafaehlers
Copy link
Author

@offordscott try with this code: add_filter( 'gravityview/fields/custom/decode_shortcodes', '__return_true' );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment