Php 计算两次重力形式WP之间的分钟数
我正在使用Wordpress和重力表单。我有两个时间域和一个数字域。数字字段以分钟为单位,必须是两次之间的分钟数 例如: 字段1(时间):03:20 字段2(时间):04:20 计算字段: 字段3(字段1和字段3之间的分钟数):60 我发现了一些非常相似的东西,但是用于计算两个日期之间天数的代码:Php 计算两次重力形式WP之间的分钟数,php,wordpress,gravity-forms-plugin,gravityforms,Php,Wordpress,Gravity Forms Plugin,Gravityforms,我正在使用Wordpress和重力表单。我有两个时间域和一个数字域。数字字段以分钟为单位,必须是两次之间的分钟数 例如: 字段1(时间):03:20 字段2(时间):04:20 计算字段: 字段3(字段1和字段3之间的分钟数):60 我发现了一些非常相似的东西,但是用于计算两个日期之间天数的代码: <?php /** * Gravity Wiz // Calculate Number of Days Between Two Gravity Form Date Fields * * Allo
<?php
/**
* Gravity Wiz // Calculate Number of Days Between Two Gravity Form Date Fields
*
* Allows you to calculated the number of days between two Gravity Form date fields and populate that number into a
* field on your Gravity Form.
*
* @version 1.1
* @author David Smith <david@gravitywiz.com>
* @license GPL-2.0+
* @link https://gravitywiz.com/calculate-number-of-days-between-two-dates/
* @copyright 2013 Gravity Wiz
*/
class GWDayCount {
private static $script_output;
function __construct( $args ) {
extract( wp_parse_args( $args, array(
'form_id' => false,
'start_field_id' => false,
'end_field_id' => false,
'count_field_id' => false,
'include_end_date' => true,
) ) );
$this->form_id = $form_id;
$this->start_field_id = $start_field_id;
$this->end_field_id = $end_field_id;
$this->count_field_id = $count_field_id;
$this->count_adjust = $include_end_date ? 1 : 0;
add_filter( "gform_pre_render_{$form_id}", array( &$this, 'load_form_script') );
add_action( "gform_pre_submission_{$form_id}", array( &$this, 'override_submitted_value') );
}
function load_form_script( $form ) {
// workaround to make this work for < 1.7
$this->form = $form;
add_filter( 'gform_init_scripts_footer', array( &$this, 'add_init_script' ) );
if( self::$script_output )
return $form;
?>
<script type="text/javascript">
(function($){
window.gwdc = function( options ) {
this.options = options;
this.startDateInput = $( '#input_' + this.options.formId + '_' + this.options.startFieldId );
this.endDateInput = $( '#input_' + this.options.formId + '_' + this.options.endFieldId );
this.countInput = $( '#input_' + this.options.formId + '_' + this.options.countFieldId );
this.init = function() {
var gwdc = this;
// add data for "format" for parsing date
gwdc.startDateInput.data( 'format', this.options.startDateFormat );
gwdc.endDateInput.data( 'format', this.options.endDateFormat );
gwdc.populateDayCount();
gwdc.startDateInput.change( function() {
gwdc.populateDayCount();
} );
gwdc.endDateInput.change( function() {
gwdc.populateDayCount();
} );
$( '#ui-datepicker-div' ).hide();
}
this.getDayCount = function() {
var startDate = this.parseDate( this.startDateInput.val(), this.startDateInput.data('format') )
var endDate = this.parseDate( this.endDateInput.val(), this.endDateInput.data('format') );
var dayCount = 0;
if( !this.isValidDate( startDate ) || !this.isValidDate( endDate ) )
return '';
if( startDate > endDate ) {
return 0;
} else {
var diff = endDate - startDate;
dayCount = diff / ( 60 * 60 * 24 * 1000 ); // secs * mins * hours * milliseconds
dayCount = Math.round( dayCount ) + this.options.countAdjust;
return dayCount;
}
}
this.parseDate = function( value, format ) {
if( !value )
return false;
format = format.split('_');
var dateFormat = format[0];
var separators = { slash: '/', dash: '-', dot: '.' };
var separator = format.length > 1 ? separators[format[1]] : separators.slash;
var dateArr = value.split(separator);
switch( dateFormat ) {
case 'mdy':
return new Date( dateArr[2], dateArr[0] - 1, dateArr[1] );
case 'dmy':
return new Date( dateArr[2], dateArr[1] - 1, dateArr[0] );
case 'ymd':
return new Date( dateArr[0], dateArr[1] - 1, dateArr[2] );
}
return false;
}
this.populateDayCount = function() {
this.countInput.val( this.getDayCount() ).change();
}
this.isValidDate = function( date ) {
return !isNaN( Date.parse( date ) );
}
this.init();
}
})(jQuery);
</script>
<?php
self::$script_output = true;
return $form;
}
function add_init_script( $return ) {
$start_field_format = false;
$end_field_format = false;
foreach( $this->form['fields'] as &$field ) {
if( $field['id'] == $this->start_field_id )
$start_field_format = $field['dateFormat'] ? $field['dateFormat'] : 'mdy';
if( $field['id'] == $this->end_field_id )
$end_field_format = $field['dateFormat'] ? $field['dateFormat'] : 'mdy';
}
$script = "new gwdc({
formId: {$this->form['id']},
startFieldId: {$this->start_field_id},
startDateFormat: '$start_field_format',
endFieldId: {$this->end_field_id},
endDateFormat: '$end_field_format',
countFieldId: {$this->count_field_id},
countAdjust: {$this->count_adjust}
});";
$slug = implode( '_', array( 'gw_display_count', $this->start_field_id, $this->end_field_id, $this->count_field_id ) );
GFFormDisplay::add_init_script( $this->form['id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script );
// remove filter so init script is not output on subsequent forms
remove_filter( 'gform_init_scripts_footer', array( &$this, 'add_init_script' ) );
return $return;
}
function override_submitted_value( $form ) {
$start_date = false;
$end_date = false;
foreach( $form['fields'] as &$field ) {
if( $field['id'] == $this->start_field_id )
$start_date = self::parse_field_date( $field );
if( $field['id'] == $this->end_field_id )
$end_date = self::parse_field_date( $field );
}
if( $start_date > $end_date ) {
$day_count = 0;
} else {
$diff = $end_date - $start_date;
$day_count = $diff / ( 60 * 60 * 24 ); // secs * mins * hours
$day_count = round( $day_count ) + $this->count_adjust;
}
$_POST["input_{$this->count_field_id}"] = $day_count;
}
static function parse_field_date( $field ) {
$date_value = rgpost("input_{$field['id']}");
$date_format = empty( $field['dateFormat'] ) ? 'mdy' : esc_attr( $field['dateFormat'] );
$date_info = GFCommon::parse_date( $date_value, $date_format );
if( empty( $date_info ) )
return false;
return strtotime( "{$date_info['year']}-{$date_info['month']}-{$date_info['day']}" );
}
}
# Configuration
new GWDayCount( array(
'form_id' => 16,
'start_field_id' => 1,
'end_field_id' => 2,
'count_field_id' => 4
) );
(函数($){
window.gwdc=函数(选项){
this.options=选项;
this.startDateInput=$(“#input_'+this.options.formId+“'+this.options.startFieldDid”);
this.endDateInput=$(“#input_'+this.options.formId+“'+this.options.endFieldId”);
this.countInput=$(“#input_'+this.options.formId+”\u'+this.options.countFieldId);
this.init=函数(){
var gwdc=此;
//为解析日期的“格式”添加数据
gwdc.startDateInput.data('format',this.options.startDateFormat);
gwdc.endDateInput.data('format',this.options.endDateFormat);
gwdc.populateDayCount();
gwdc.startDateInput.change(函数(){
gwdc.populateDayCount();
} );
gwdc.endDateInput.change(函数(){
gwdc.populateDayCount();
} );
$('#ui datepicker div').hide();
}
this.getDayCount=函数(){
var startDate=this.parseDate(this.startDateInput.val(),this.startDateInput.data('format'))
var endDate=this.parseDate(this.endDateInput.val(),this.endDateInput.data('format');
var dayCount=0;
如果(!this.isValidDate(开始日期)| |!this.isValidDate(结束日期))
返回“”;
如果(开始日期>结束日期){
返回0;
}否则{
var diff=结束日期-开始日期;
dayCount=diff/(60*60*24*1000);//秒*分钟*小时*毫秒
dayCount=Math.round(dayCount)+this.options.countAdjust;
返回天数;
}
}
this.parseDate=函数(值、格式){
如果(!值)
返回false;
格式=格式.拆分(“”“);
var dateFormat=格式[0];
变量分隔符={斜杠:'/',短划线:'-',点:'.'};
var separator=format.length>1?分隔符[格式[1]]:separators.slash;
var dateArr=value.split(分隔符);
开关(日期格式){
案例“mdy”:
返回新日期(dateArr[2],dateArr[0]-1,dateArr[1]);
案例“dmy”:
返回新日期(dateArr[2],dateArr[1]-1,dateArr[0]);
案例“ymd”:
返回新日期(dateArr[0],dateArr[1]-1,dateArr[2]);
}
返回false;
}
this.populateDayCount=函数(){
this.countInput.val(this.getDayCount()).change();
}
this.isValidDate=函数(日期){
return!isNaN(Date.parse(Date));
}
this.init();
}
})(jQuery);
我们(Gravity Wiz)有一个名为GF日期时间计算器的插件。可向Gravity Perks许可证持有人索取。这里有一个关于它如何工作的快速视频
欢迎来到堆栈溢出。不清楚你想要什么。请向平台上的用户提供您试图实现的目标、到目前为止您尝试了什么以及在尝试时遇到的任何错误。这一点再清楚不过了。我希望间隔几分钟,而不是两天之间的几天。
class GWDayCount {
private static $script_output;
function __construct( $args ) {
extract( wp_parse_args( $args, array(
'form_id' => false,
'start_field_id' => false,
'end_field_id' => false,
'count_field_id' => false,
'include_end_date' => true,
) ) );
$this->form_id = $form_id;
$this->start_field_id = $start_field_id;
$this->end_field_id = $end_field_id;
$this->count_field_id = $count_field_id;
$this->count_adjust = $include_end_date ? 1 : 0;
add_filter( "gform_pre_render_{$form_id}", array( &$this, 'load_form_script') );
add_action( "gform_pre_submission_{$form_id}", array( &$this, 'override_submitted_value') );
}
function load_form_script( $form ) {
// workaround to make this work for < 1.7
$this->form = $form;
add_filter( 'gform_init_scripts_footer', array( &$this, 'add_init_script' ) );
if( self::$script_output )
return $form;
?>
<script type="text/javascript">
(function($){
window.gwdc = function( options ) {
this.options = options;
this.startDateInput = $( '#input_' + this.options.formId + '_' + this.options.startFieldId );
this.endDateInput = $( '#input_' + this.options.formId + '_' + this.options.endFieldId );
this.countInput = $( '#input_' + this.options.formId + '_' + this.options.countFieldId );
startDateInput1 = $( '#input_' + this.options.formId + '_' + this.options.startFieldId );
endDateInput2 = $( '#input_' + this.options.formId + '_' + this.options.endFieldId );
this.init = function() {
var gwdc = this;
// add data for "format" for parsing date
gwdc.startDateInput.data( 'format', this.options.startDateFormat );
gwdc.endDateInput.data( 'format', this.options.endDateFormat );
gwdc.populateDayCount();
gwdc.startDateInput.change( function() {
gwdc.populateDayCount();
} );
gwdc.endDateInput.change( function() {
gwdc.populateDayCount();
} );
$( '#ui-datepicker-div' ).hide();
}
this.getDayCount = function() {
var startDate = this.parseDate( this.startDateInput.val(), this.startDateInput.data('format') )
min1 = startDateInput1;
var endDate = this.parseDate( this.endDateInput.val(), this.endDateInput.data('format') );
var dayCount = 0;
if( !this.isValidDate( startDate ) || !this.isValidDate( endDate ) )
return 'neki ni kul';
if( startDate > endDate ) {
return 0;
} else {
var diff = endDate - startDate;
dayCount = diff ;
var test = min1;
dayCount = Math.round( dayCount ) + this.options.countAdjust;
return test;
}
}
this.parseDate = function( value, format ) {
if( !value )
return false;
format = format.split('_');
var dateFormat = format[0];
var separators = { slash: ':', dash: '-', dot: '.' };
var separator = format.length > 1 ? separators[format[1]] : separators.slash;
var dateArr = value.split(separator);
switch( dateFormat ) {
case 'mdy':
return new Date( 0, dateArr[0] - 1, dateArr[1] );
case 'dmy':
return new Date( 0, dateArr[1] - 1, dateArr[0] );
case 'ymd':
return new Date( dateArr[0], dateArr[1] - 1, 0 );
}
return false;
}
this.populateDayCount = function() {
this.countInput.val( this.getDayCount() ).change();
}
this.isValidDate = function( date ) {
return !isNaN( Date.parse( date ) );
}
this.init();
}
})(jQuery);
</script>
<?php
self::$script_output = true;
return $form;
}
function add_init_script( $return ) {
$start_field_format = false;
$end_field_format = false;
foreach( $this->form['fields'] as &$field ) {
if( $field['id'] == $this->start_field_id )
$start_field_format = $field['dateFormat'] ? $field['dateFormat'] : 'mdy';
if( $field['id'] == $this->end_field_id )
$end_field_format = $field['dateFormat'] ? $field['dateFormat'] : 'mdy';
}
$script = "new gwdc({
formId: {$this->form['id']},
startFieldId: {$this->start_field_id},
startDateFormat: '$start_field_format',
endFieldId: {$this->end_field_id},
endDateFormat: '$end_field_format',
countFieldId: {$this->count_field_id},
countAdjust: {$this->count_adjust}
});";
$slug = implode( '_', array( 'gw_display_count', $this->start_field_id, $this->end_field_id, $this->count_field_id ) );
GFFormDisplay::add_init_script( $this->form['id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script );
// remove filter so init script is not output on subsequent forms
remove_filter( 'gform_init_scripts_footer', array( &$this, 'add_init_script' ) );
return $return;
}
function override_submitted_value( $form ) {
$start_date = false;
$end_date = false;
foreach( $form['fields'] as &$field ) {
if( $field['id'] == $this->start_field_id )
$start_date = self::parse_field_date( $field );
if( $field['id'] == $this->end_field_id )
$end_date = self::parse_field_date( $field );
}
if( $start_date > $end_date ) {
$day_count = 0;
} else {
$diff = $end_date - $start_date;
$day_count = $diff; // secs * mins * hours
$day_count = round( $day_count ) + $this->count_adjust;
}
$_POST["input_{$this->count_field_id}"] = $day_count;
}
static function parse_field_date( $field ) {
$date_value = rgpost("input_{$field['id']}");
$date_format = empty( $field['dateFormat'] ) ? 'mdy' : esc_attr( $field['dateFormat'] );
$date_info = GFCommon::parse_date( $date_value, $date_format );
if( empty( $date_info ) )
return false;
return strtotime( "{$date_info['year']}-{$date_info['month']}-{$date_info['day']}" );
}
}