Skip to content

Instantly share code, notes, and snippets.

@rafaehlers
Created June 18, 2021 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafaehlers/5bc66e2044ed15f2cefc941ad0fd2429 to your computer and use it in GitHub Desktop.
Save rafaehlers/5bc66e2044ed15f2cefc941ad0fd2429 to your computer and use it in GitHub Desktop.
Shortcode that receives a date field and calculates it with another date using relative dates
<?php // DO NOT COPY THIS LINE
//USAGE: [gv_expiration date="2" duration="+1 year"]
// 2 is the field ID of the date field
// If original date is "2021-04-01" it will output "2022-04-01"
// https://docs.gravityview.co/article/79-using-relative-start-dates-and-end-dates
add_shortcode( 'gv_expiration', 'gv_expiration' );
function gv_expiration( $atts ) {
global $gravityview_view;
extract($gravityview_view->field_data); // create a $entry variable with current entry data array
extract( shortcode_atts(
array(
'date' => '',
'duration' => '',
), $atts )
);
$date = $entry[$date];
if (empty($date) || empty($duration)) {
return 'Error: No field ID specified.';
}else{
$expiration = date('Y-m-d', strtotime($date.' '.$duration));
}
return $expiration;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment