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