0

I am using acf_form() function to add/update posts frm the front-end. Everything works fine except the post date. Somehow it is posting a very old date (1970/01/01). I wanted to post the current date instead. Here is my code:

    $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" />',
    ));

I know that I don't need to set 'post_modified' and 'post_modified_gmt' values if it is for the current date. I tried without those 2, then I tried setting current datetime manually. But it always inserted that weird old date. I am having same problem for updating a post.

Imrul.H
  • 5,760
  • 14
  • 55
  • 88

2 Answers2

2

WordPress has a number of date/time functions

https://codex.wordpress.org/Formatting_Date_and_Time

you should need is:

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" />',
));

for more info :

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

yogesh chatrola
  • 429
  • 4
  • 11
0

It looks like 1970/01/01 is your system date. Check out your configuration.

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().

Marat Badykov
  • 824
  • 4
  • 8