You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress中Contact Form 7自定义_datetime邮件标签时区错误问题求助

Fixing the Timezone Issue for Custom CF7 _datetime Mail Tag

I see the problem here—your custom _datetime tag is pulling the UTC timestamp but not correctly converting it to your WordPress-configured timezone, even though you’ve set it in Settings > General. Let’s fix that with a couple of reliable approaches:

wp_date() is a modern WordPress function introduced in 5.3 that inherently respects your site’s timezone settings, making it more straightforward than date_i18n for this use case. Replace your existing code with this:

function add_custom_mail_tag( $custom_mail_tags, $form_id ) {
    $custom_mail_tags[] = "_datetime";
    return $custom_mail_tags;
}
add_filter( "wpcf7_special_mail_tags", function( $output, $name, $html ) {
    if ( $name === "_datetime" ) {
        $submission = WPCF7_Submission::get_instance();
        if ( $timestamp = $submission->get_meta( 'timestamp' ) ) {
            // Use wp_date which automatically applies your site's timezone
            $date = wp_date( get_option( 'date_format' ), $timestamp );
            $time = wp_date( get_option( 'time_format' ), $timestamp );
            $date_time = $date . " " . $time;
            return $date_time;
        }
    }
    return $output;
}, 10, 3 );

Solution 2: Explicit Timezone Conversion (Backward Compatible)

If you’re running an older WordPress version (pre-5.3), use PHP’s DateTimeImmutable class to explicitly convert the UTC timestamp to your site’s timezone. This method leaves no room for timezone ambiguity:

function add_custom_mail_tag( $custom_mail_tags, $form_id ) {
    $custom_mail_tags[] = "_datetime";
    return $custom_mail_tags;
}
add_filter( "wpcf7_special_mail_tags", function( $output, $name, $html ) {
    if ( $name === "_datetime" ) {
        $submission = WPCF7_Submission::get_instance();
        if ( $timestamp = $submission->get_meta( 'timestamp' ) ) {
            // Get your WordPress-configured timezone
            $wp_timezone = wp_timezone();
            
            // Create a UTC datetime object from the timestamp
            $utc_datetime = DateTimeImmutable::createFromFormat(
                'U', 
                (string) $timestamp, 
                new DateTimeZone( 'UTC' )
            );
            
            // Convert to your site's timezone
            $local_datetime = $utc_datetime->setTimezone( $wp_timezone );
            
            // Format using your site's date/time settings
            $date = $local_datetime->format( get_option( 'date_format' ) );
            $time = $local_datetime->format( get_option( 'time_format' ) );
            
            $date_time = $date . " " . $time;
            return $date_time;
        }
    }
    return $output;
}, 10, 3 );

Why This Works

  • CF7 stores the submission timestamp as a Unix UTC timestamp (from time()), so we need to explicitly convert it to your site’s timezone.
  • wp_timezone() pulls the exact timezone you set in WordPress Settings > General, ensuring consistency with core and CF7’s built-in tags.
  • The DateTimeImmutable approach is bulletproof for older WP versions where wp_date() isn’t available, as it directly handles timezone conversions without relying on potentially inconsistent global timezone settings.

Test either version, and your [_datetime] tag should now output the correct local time matching your WordPress timezone settings.

内容的提问来源于stack exchange,提问作者Majid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 12:12:40