Home › Forums › General Issues › Hook/Filter to edit the timestamp after user has selected a date and time
Hi
I have a form for users to create a new post and on there, there is a date and time selector. Is there a hook/filter for me to edit this based on a time zone once a user has submitted the form as I have users around the world and I want them to select the date and time in their local time.
Update: So I have found this http://www.advancedcustomfields.com/resources/acfsave_post/.
I am hooking into it, but the date isn’t actually being written to the database. Not sure why not. Here is my code:
function updateSeminarTime( $post_id ) {
// bail early if no ACF data
if( empty($_POST['acf']) ) {
return;
}
// array of field values
//$fields = $_POST['acf'];
// specific field value
$post = get_post( $post_id );
$expertId = $post->post_author;
$userTimeZone = getTimeZone($expertId);
$userTimeZoneOffset = get_timezone_offset($userTimeZone,'UTC');
$seminarDate = $_POST['acf']['seminar_date'];
$seminarDate = strtotime($seminarDate);
$seminarDate = $seminarDate + $userTimeZoneOffset;
$seminarDate = date('m/d/y h:mm tt',$seminarDate);
$_POST['acf']['seminar_date'] = $seminarDate;
}
// run before ACF saves the $_POST['acf'] data
add_action('acf/save_post', 'updateSeminarTime', 1);
I’m wondering if I am writing the format wrong. BTW I have tried with date() and formatting it to a string and also just as a UNIX timestamp.
Hi @ch1n3s3b0y
ACF saves the value to the DB in the format you set in the field setting.
Have you made sure you’re resaving the value in the same format?
Hi @jonathan
I was trying to intercept the form submission before it was actually written to the DB using the hook/filter above. Is that not what this filter is for? I know it is doing something as the datetime is no longer written into the DB, it is just blank. Do I then need to update (SQL) that field? Seems like there should be a way to simply update the $_POST['acf']['seminar_date']
before it is inserted into the DB.
Update:
@jonathan in my DB ACF is saving the datetime as a UNIX timestamp. I have edited the code to use update_field() and it still isn’t saving to the database, using the field name or field key.
function updateSeminarTime($post_id)
{
// bail early if no ACF data
if (empty($_POST['acf'])) {
return;
}
// array of field values
//$fields = $_POST['acf'];
// specific field value
$post = get_post($post_id);
$expertId = $post->post_author;
$userTimeZone = getTimeZone($expertId);
$userTimeZoneOffset = get_timezone_offset($userTimeZone, 'UTC');
$seminarDate = $_POST['acf']['seminar_date'];
$seminarDate = strtotime($seminarDate);
$seminarDate = $seminarDate + $userTimeZoneOffset;
//$seminarDate = date('m/d/y h:mm tt', $seminarDate);
echo $seminarDate;
echo $userTimeZoneOffset;
echo $post_id;
update_field('field_552cb019acd87',$seminarDate,$post_id);
}
// run before ACF saves the $_POST['acf'] data
add_action('acf/save_post', 'updateSeminarTime', 20);
When I save the post, it automatically takes me to the new post. How can I debug this and see any echo’d values etc?
I have now narrowed down the issue to update_field(). Here is mine:
update_field('field_552cb019acd87',$seminarDate,353);
This isn’t working. $seminarDate is a timestamp. Can anyone see any reason why this wouldn’t work?
Ok, update_field() wasn’t working for me, even though I’m sure it was right. I am instead using $wpdb to update the field in the database, which is working. I’m not marking this as resolved as the issue with update_field() still remains. If anyone could shed any light on this please let me know.
Hi @ch1n3s3b0y
You’ve done some great testing!
I can’t say why update_field would not work for you.. the parameters looks correct to me.
However instead of a custom $wpdb update you can try using WP cores update_post_meta()
function. It will essentially do the same thing as update_field()
.
https://codex.wordpress.org/Function_Reference/update_post_meta
The topic ‘Hook/Filter to edit the timestamp after user has selected a date and time’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.