Support

Account

Home Forums General Issues Calculating time in ACF

Solved

Calculating time in ACF

  • Hi I have hunted around for this to find a simpletons solution for the problem but to no avail.
    I have two times – start time and end time.
    I want to simply auto calculate the duration between the two and then it be add and stored in a field in my form.

    Any ideas on how to achieve this would be really great. Thank you in advance

  • There isn’t enough information here to give an answer for your needs.

    Where are you trying to do this?

    What do you expect to happen?

    Does it need to be done in PHP? you see the value after saving? == simple

    Does it need to be done in JavaScript? You see the value after selecting dates? == difficult

  • Hi sorry lack of information is down to my lack of knowledge around this sadly.

    Ok so I have a job form on the front end.
    The employee sets the time started and the time ending.
    I would like them to instantly see the number of hours worked, although if it’s easier to calculate on saving then that’s great too
    This result would be saved in a field named total hours to be retained for use on other forms such as billing etc.

    Hope this helps John

  • 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.

  • Hi John

    You work ever so hard, so many replies to so many people you deserve a medal.

    Thank you for the detailed answer, it looks like I go down the PHP route. I’ll have a play with the code you posted I assume it goes in functions.php?

    BTW the way I have wondered as I have seen it in other pieces of code, what do the numbers mean at the end of add action element in this instance 20?

    Thank you once again

  • That is the priority that your filter is called. All of the built in ACF filters and actions are called with a priority of 10. So if you want to run your before ACF then you use a number < 10 and if you want them to run after you use a priority > 10. Since we want ACF to have already saved the fields when we try to get them we want > 10 in this case.

    I would try to figure out the JS for more help, I’m sure the code would come in handy in the future, if I had time to kill, but as development goes, I am going through a peak at the moment. I come here and answer questions when I need to give my brain a break from whatever I’m working on.

  • Hi John

    You’re a diamond and thank you so much for your help once again it’s really appreciated

  • Hi John

    I gave up in the end I used the following but it returns only a -1 in my number field. I’ll have to try again another time thanks for your help though.

    `add_action(‘acf/save_post’, ‘calc_time’, 1);
    function calc_time($post_id) {
    $start = get_field(‘consultation_start_time’, $post_id);
    $end = get_field(‘consultation_end_time’, $post_id);
    // calculate difference
    // see https://stackoverflow.com/questions/10557076/adding-two-time-values-of-similar-formats-using-php

    $calculated_time = ($end.” -“.$start);

    update_field(‘total_duration’, $calculated_time, $post_id);
    }

  • SOLVED

    `function calculated_field_value() {
    // Get field values (these will be updated in real-time)
    $starttime = af_get_field( ‘consultation_start_time’ );
    $endtime = af_get_field( ‘consultation_end_time’ );

    // Calculate the sum of the current field values
    $starttimestamp = strtotime($starttime);
    $endtimestamp = strtotime($endtime);
    $difference = abs($endtimestamp – $starttimestamp)/3600;

    // Display the sum in the calculated field
    return $difference . ‘hrs<br><small>NB – Number is a decimal of hours and minutes</small>’;
    }
    add_filter( ‘af/field/calculate_value/name=total_duration’, ‘calculated_field_value’, 10, 0 );

  • Was having a similar issue. This worked for me. Thanks!

  • Not a problem surprised I can help anyone lol.

    all the same here is another bit of code that will output hours and minutes rather than decimal time.

    		 <?php
    			$starttime = get_field( 'consultation_start_time' );
    			$endtime = get_field( 'consultation_end_time' );
    			$starttimestamp = strtotime($starttime);
    			$endtimestamp = strtotime($endtime);
    			$difference = abs($endtimestamp - $starttimestamp);
    
    			echo "Duration in time: " . date("h:i", $difference)."</br></br>";
    ?> 

    PLUS

    This version will output decimal time to only 2 decimal places.

    <?php
    			  // Get field values (these will be updated in real-time)
    			  $starttime = get_field( 'consultation_start_time' );
    			  $endtime = get_field( 'consultation_end_time' );
    
    			  // Calculate the sum of the current field values
    			$starttimestamp = strtotime($starttime);
    				$endtimestamp = strtotime($endtime);
    				$difference = abs($endtimestamp - $starttimestamp)/3600;
    
    			  // Display the sum in the calculated field
    			  echo "Duration decimal time: " . number_format ( $difference,2 ) . ' hrs<br><small>NB - Number is a decimal of hours and minutes<br><br></small>';
    			?>
Viewing 11 posts - 1 through 11 (of 11 total)

The topic ‘Calculating time in ACF’ is closed to new replies.