Home › Forums › General Issues › Calculating time in ACF › Reply To: Calculating time in ACF
To do this in PHP you would create an acf/save_post filter, get the two fields, calculate the difference and then update the 3rd field.
add_action('acf/save_post', 'calc_time', 20);
function calc_time($post_id) {
$start = get_field('start_time', $post_id);
$end = get_field('end_time', $post_id);
// calculate difference
// see https://stackoverflow.com/questions/10557076/adding-two-time-values-of-similar-formats-using-php
update_field('total_time', $calculated_time, $post_id);
}
Doing this with JS would be much more difficult and I would not be able to provide any code without spending a lot of time doing so because there are no examples that I know of. YOu would need to add custom JS https://www.advancedcustomfields.com/resources/adding-custom-javascript-fields/ and add onChange actions to the date fields. The you would need to use the ACF JS API https://www.advancedcustomfields.com/resources/javascript-api/ to get the values of the fields, then you’d need to calulate this difference using JS https://stackoverflow.com/questions/12193342/calculate-time-difference-in-hhmm-format and finally use the ACF API to set the value of the 3rd field.
Sorry I can’t help you more with the code, but like I said, I don’t have any examples and building them would require quite an effort.
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.