Last active
November 14, 2017 18:04
-
-
Save rafaehlers/973c0beee6b95606a9640bf3c45ec929 to your computer and use it in GitHub Desktop.
Calculate days between two date fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@offordscott try with this code:
add_filter( 'gravityview/fields/custom/decode_shortcodes', '__return_true' );