WordPress中Contact Form 7自定义_datetime邮件标签时区错误问题求助
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:
Solution 1: Use wp_date() (Recommended for WordPress 5.3+)
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
DateTimeImmutableapproach is bulletproof for older WP versions wherewp_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

