Skip to content

Instantly share code, notes, and snippets.

@plugin-republic
Created November 3, 2025 13:46
Show Gist options
  • Select an option

  • Save plugin-republic/8687c74264e562fa93cb6cbdde373f91 to your computer and use it in GitHub Desktop.

Select an option

Save plugin-republic/8687c74264e562fa93cb6cbdde373f91 to your computer and use it in GitHub Desktop.
<?php
/**
* Update the three IDs below with your own field IDs
*/
function demo_get_start_date_field_id() {
return 1245; // The ID of the date field for the start date
}
function demo_get_end_date_field_id() {
return 1246; // The ID of the date field for the end date
}
function demo_get_result_field_id() {
return 1247; // The ID of the number field for the difference
}
function demo_date_difference() {
?>
<script>
( function( $ ) {
var date_difference = {
init: function() {
var result_field_id = <?php echo demo_get_result_field_id(); ?>;
$( '.pewc-number-field-' + result_field_id ).attr( 'readonly', true );
$( 'body' ).on( 'change update','.pewc-date-field', this.check_difference );
},
check_difference: function() {
// pewc-date-field-1246
var start_field_id = <?php echo demo_get_start_date_field_id(); ?>;
var end_field_id = <?php echo demo_get_end_date_field_id(); ?>;
var start_date = $( '.pewc-date-field-' + start_field_id ).val();
var end_date = $( '.pewc-date-field-' + end_field_id ).val();
if( start_date && end_date ) {
start_date = new Date( start_date );
end_date = new Date( end_date );
diff_time = Math.abs( end_date - start_date );
diff_days = Math.floor( diff_time / ( 1000 * 60 * 60 * 24 ) );
$( '.pewc-number-field-' + <?php echo demo_get_result_field_id(); ?> ).val( diff_days );
}
}
}
date_difference.init();
})( jQuery );
</script>
<?php
}
add_action( 'wp_footer', 'demo_date_difference' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment