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>';
?>
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 );
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);
}
Hi John
You’re a diamond and thank you so much for your help once again it’s really appreciated
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
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
Hi John thanks for that I’m using a relationship field
Just for the record I have updated to V5.7.1 of ACF. Bought the developer license to see if that fixed it.
No joy still only shows fields under PHP V5.3
It’s something to do with this code for certain
if(get_field('name'))
$field_name = "name";
if(empty($field_name['name'])){
echo " ";}
else
....
Hi John, I upgraded to V7 from 5.3. I did it sometime ago but never realised the ACF data was missing it was just something I did.
All the same winding back to V5.3 ensures everything is working fine.
I am using the latest version of ACF too Version 4.4.12
I have also double checked the fields show on PHP V5.3 but not 5.4, 5.5, 5.6 or 7
I’ve taken your advice and submitted a bug report its all a bit odd.
I have had a look into this and it seems I am using an if statement that has since been changed since PHP V5.3.
I other words in PHP V5.3 this code shows all the ACF field data
if(get_field('name'))
$field_name = "name";
if(empty($field_name['name'])){
echo " ";}
else
....
However if you upgrade from 5.3 upward this statement stops the PHP fields showing … take the ‘if’ statement out it all shows again.
So it seems I need to upgrade this ‘if’ statement somehow
Thank you so much for this … I think I’ve got something properly messed up somewhere.
I’ll have a play around with it all.
Hi cadoomunhoz
Thanks for that posting. I tried this but the other way round class after the :
The way I did it, the css was written to the database along with the value which in effect makes the data wrong if it needs to be used for something else.
Can you confirm if your css classes have been written to your database .. maybe I did it all the wrong way round.
Hi John
I’m using a basic genesis theme.
I use it will go in functions.php however Genesis doesn’t seem to like additional PHP code unless it’s added as new function. So not being an expert in PHP or Genesis I’m a little stumped at the mo.
It’s no problem though don’t want anyone putting themselves out as I am a novice at coding and this website I’m building is just a hobby to get myself learning.
All the same really appreciate the feedback and info from everyone.
Hi John
Thank you for the comprehensive reply. I do feel having read @giovannicold‘s reply again they are on the right lines. I have tried the solution you offered and the span class value are also stored in the database which is something I don’t want as these also get passed to my filter system which looks shocking.
If I could figure out how and where to apply @giovannicold‘s fix then I would give it a shot but I can’t seem to get it to work in my theme.
I’ll do some more reading up I think, thank you once again for your reply. Maybe this one requires someone a little more experienced than me to tackle. Thanks again.
Hi I don’t do Skype and think it would be more useful for everyone in the ACF world that you share the solution here.
Thank you though for the offer and congratulations on finding a solution. In the meantime I’ll keep looking
I’ll tell you what guys if we could get to the bottom of how to do this then i feel it would change ACF for the better beyond belief.
Why on earth there isn’t another box next to each field to assign a CSS class or ID I have no idea.
To be able to style a field and it’s value easily is essential in my view.
I have no idea why if I have a selector field or check box field I can’t make the style of each value different.
I want to do simple stuff like show an image of sun if a user ticks the value for ‘Full Sun’.
Honestly it would change the front end of my website beyond belief.
I hope somehow this can happen easily without ooooodles of code
Hi thank you for your kind suggestion. I’m not sure what you mean though.
Were would I add this and what could it look like in terms of the code.
Sorry for being thick but still trying to learn a lot of this stuff.
Hi it seems like you can wrap the value in a class. I just tried it and it works fine for styling.
It does however store the class in the database along with the value. I wonder if there is a way of applying a class to the value without it being stored with the value?
Hi thank you
Does this also mean I can wrap the value in a css class too. For instance <div class=”redflower”>Red</div>
and so on for each colour
If so does this effect the ability to filter the values at a later date or even search for a red flower for instance
Thanks for the reply. I probably haven’t made myself clear. I want to try and make the front end look something like this page if you look at the colour section.
Each colour has been displayed as a colour box with an icon in it.
Obviously in the edit post page someone will have selected the word Red or Green, or as you have pointed out they could have used a limited colour palette. But on the front end my output simply says Red or Blue or Green and I would like it to display a graphic box showing the colour as this page does.
Any help would be great
Sorted this myself.
For the record I have created a shortcode.php file and called it from functions.php
It was quite easy once I understood. This is the type of code I used
function cultivar_name() {
if(get_field(‘cultivar’)){
$field_name = “cultivar”;
$field = get_field_object($field_name);
echo ‘‘, $field[‘label’],’‘ . ‘: ‘ . ‘<div class=”headlinename”>’, $field[‘value’],'</div>’,'</br>’;}
}
add_shortcode( ‘cultivar’, ‘cultivar_name’ );
Hi this is what I have used
if(get_field(‘cultivar’)){
$field_name = “cultivar”;
$field = get_field_object($field_name);
echo ‘‘, $field[‘label’],’‘ . ‘: ‘ . ‘<div class=”headlinename”>’, $field[‘value’],'</div>’,'</br>’;}
The code has a div wrapped round the value so you can use ::after content: ‘miles’; if you liked.
This piece of code only shows when there is a value too.
Hi I havent had experience with this but I had trouble creating a single.php in genesis (which I know it a little different) all the same the result was the same.
However I am using Beaver Builder themer and it works really well – similar to visual composer I think
Hope this helps
An update on this … I moved my theme development to Beaver Builder Themer instead and it seems to be working
Thank you for the reply, I was looking for a way to do it in CSS rather than PHP I have a filter script that output the results. So I was looking to have a way of adding styling the background per result in CSS
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.