Support

Account

Home Forums General Issues Calculating time in ACF Reply To: Calculating time in ACF

  • 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>';
    			?>