前端添加 post 当前日期不起作用

Front-end add post current date is not working

我正在使用 acf_form() 函数从前端 add/update posts。除了 post 日期外,一切正常。不知何故,它 post 是一个非常古老的日期 (1970/01/01)。我想 post 当前日期。这是我的代码:

    $current_datetime = date('Y-m-d H:i:s');
    acf_form_head();
    acf_form(array(
        'post_id'       => 'new_post',
        'post_title'    => true,
        'post_content'  => true,
        'submit_value'  => __("Send", 'acf'),
        'updated_message' => __("Suksess!", 'acf'),
        'new_post'      => array(
            'post_type'     => 'nyhet',
            'post_status'   => 'publish',
            'post_author'   => get_current_user_id(),
            'post_category' => '',
            'post_modified' => $current_datetime,
            'post_modified_gmt' => $current_datetime,
        ),
        'fields'        => array('ingress', 'publisere_kun_pa_lokallagssiden', 'featured_image'),
        'html_submit_button' => '<input type="submit" class="button box-button green save-content" value="%s" />',
    ));

我知道如果是当前日期,我不需要设置 'post_modified' 和 'post_modified_gmt' 值。我试过没有那些 2,然后我尝试手动设置当前日期时间。但它总是插入那个奇怪的旧日期。我在更新 post.

时遇到了同样的问题

看起来 1970/01/01 是您的系统日期。检查您的配置。

http://php.net/manual/en/function.date.php

Function date returns a string formatted according to the given format string using the given integer timestamp or the current system time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time().

WordPress 有许多 date/time 功能

https://codex.wordpress.org/Formatting_Date_and_Time

你应该需要的是:

acf_form_head();
acf_form(array(
    'post_id'       => 'new_post',
    'post_title'    => true,
    'post_content'  => true,
    'submit_value'  => __("Send", 'acf'),
    'updated_message' => __("Suksess!", 'acf'),
    'new_post'      => array(
        'post_type'     => 'nyhet',
        'post_status'   => 'publish',
        'post_author'   => get_current_user_id(),
        'post_category' => '',
        'post_modified' => current_time( 'mysql' ),
        'post_modified_gmt' => current_time( 'mysql' ),
    ),
    'fields'        => array('ingress', 'publisere_kun_pa_lokallagssiden', 'featured_image'),
    'html_submit_button' => '<input type="submit" class="button box-button green save-content" value="%s" />',
));

了解更多信息:

https://developer.wordpress.org/reference/functions/current_time/