Home › Forums › General Issues › Ninja Forms and ACF Date/Time Picker Not Behaving
I’ve got a Ninja Form with a Date/Time Picker. I am using update_field
to save the value in the database, but it will not work correctly and instead saves the value as:
a:4:{s:4:"date";s:10:"2023-03-07";s:4:"hour";s:2:"04";s:6:"minute";s:2:"30";s:4:"ampm";s:2:"pm";}
This breaks the database record since this is not a properly formatted Date/Time – which should be converted to YYYY-MM-DD HH:II:SS
in the database by update_field
What’s weird to me, is that if it’s a Date picker only, the update_field
function works just fine. So it appears the issue is with the Time part.
It’s infuriating.
Is the first value what NF is returning?
If that’s the case then you will need to convert that to a proper date time value (YYYY-MM-DD HH:II:SS) before calling update_field(). ACF will not do this for you.
I created a debug
field to display the value of fields, and yes … that is what ACF is saving using update_field
It works fine as a Date
only, but as soon as Time
is included, it breaks.
when you call update_field() acf will save whatever value you pass to it without altering that value.
You’d think so… but update_field
has no problem taking the {s:4:"date";s:10:"2023-03-07";
part and converting that to a proper YYYY-MM-DD
value in the database. I’ve tested that extensively.
The only time that ACF does anything with the value is when it is formatted for display. During this process ACF does $unixtimestamp = strtotime( $value );
. I am assuming that somehow strtotime() is able to extract the date.
Ok, that makes sense.
So, considering my initial post, is there a way to convert that string to a proper Date/Time
value that update_posts
and subsequently ACF can use?
I appreciate your assistance.
Is the value you are trying to insert an array or is it the serialized value you’re seeing in the db.
Before updating var_dump($value);
It’s a serialized value that is being saved. So, I need to confirm that’s always going to be the case with Time
, so I can do something with that serialized value and properly save it using update_field
WP automatically serializes arrays when inserted into the DB, so you need to know if it’s an array or it’s already serialized. I’m guessing that it’s an array because if not I would expect to see a double serialization, however, that does not always happen and is no guarantee.
// if the value is serialized do this
$date = unserialize($date);
// always do this
$date = $date['date'].' '.$date['hour'].':'.$date['minute'].$date['ampm'];
$date = date('Y-m-d H:i:s', strtotime($date));
Ok, you were correct. It was an array all along and needed to be properly separated. In my case, I was originally intending to record the Date
and Time
separately, so this helps me solve how to properly do that.
The Date was never an issue, because the format is controllable from within Ninja Forms, but the Time is needs to be formatted correctly after the fact.
You must be logged in to reply to this topic.
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.