跳转到主要内容

日期时间控件

Elementor 日期时间控件基于 Flatpickr 库显示一个日期/时间选择器字段。

该控件定义在继承 Base_Data_Control 类的 Control_Date_Time 类中。

使用此控件时,应将 type 设置为 \Elementor\Controls_Manager::DATE_TIME 常量。

参数

名称 类型 默认值 描述
type string date_time 控件的类型。
label string 显示在字段上方的标签。
description string 显示在字段下方的描述。
show_label bool true 是否显示标签。
label_block bool true 是否在单独一行显示标签。
separator string default 设置控件分隔符的位置。可用值为 default、before 和 after。default 将隐藏分隔符,除非控件类型有特定的分隔符设置。before / after 会将分隔符定位在控件之前/之后。
picker_options array enableTime:true,
minuteIncrement:1
选择器的配置。
default string 默认日期/时间,使用 MySQL 格式 (YYYY-mm-dd HH:ii)。

Return Value

(string) The chosen date/time in MySQL format (YYYY-mm-dd HH:ii).

Usage

<?php
class Elementor_Test_Widget extends \Elementor\Widget_Base {

	protected function register_controls(): void {

		$this->start_controls_section(
			'content_section',
			[
				'label' => esc_html__( 'Content', 'textdomain' ),
				'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
			]
		);

		$this->add_control(
			'due_date',
			[
				'label' => esc_html__( 'Due Date', 'textdomain' ),
				'type' => \Elementor\Controls_Manager::DATE_TIME,
			]
		);

		$this->end_controls_section();

	}

	protected function render(): void {
		$settings = $this->get_settings_for_display();

		if ( empty( $settings['due_date'] ) ) {
			return;
		}

		$due_date = strtotime( $settings['due_date'] );
		$due_date_in_days = $due_date / DAY_IN_SECONDS;
		?>
		<p><?php printf( esc_html__( 'Something will happen in %s days.', 'textdomain' ), $due_date_in_days ); ?></p>
		<?php
	}

	protected function content_template(): void {
		?>
		<#
		if ( '' === settings.due_date ) {
			return;
		}

		const due_date = new Date( settings.due_date );
		const now_date = new Date();
		const due_date_in_days = Math.floor( ( due_date - now_date ) / 86400000 ); // 86400000 milliseconds in one Day.
		#>
		<p> Something will happen in {{{ due_date_in_days }}} days. </p>
		<?php
	}

}