[Résolu] Calculate time between two custom date/time fields
This support ticket is created Il y a 3 années et 11 mois. There's a good chance that you are reading advice that it now obsolete.
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.
Hello, there is nothing built-in to Toolset that will calculate values like this, so you would need custom PHP code. Toolset Types date fields store Unix timestamp values, so you would need to understand how to calculate time differences with PHP using Unix timestamps. Something like the shortcode shared in this ticket: https://toolset.com/forums/topic/create-deadline-filed-and-add-it-to-post-grid/
Did you have a specific question about those changes? Generally speaking, I have this feedback for you:
1. Do not use "-" characters in PHP variable names. See https://www.php.net/manual/en/language.variables.basics.php
2. You have used the format "h:i" with the start time Types field value, but that only outputs the hour (12-hour clock) and minutes of each date/time. So let's say the start time is one in the afternoon and the end time is one in the morning the following day. The outputs of each shortcode would be:
01:00
and
01:00
If you calculate the difference between dates generated from only these two values, the result is zero hours and zero minutes. In other words, you haven't given the date difference calculation function enough information by outputing only h:i from each field. Check these datetime format, interface, and diff function documentation links: https://www.php.net/manual/en/datetime.format.php https://www.php.net/manual/en/class.datetimeinterface.php lien caché
You must generate more accurate datetime objects to calculate time, using the format and reference documents to output the Types field date values in the proper format.
3. Without providing an "end" datetime value in the format_date_difference shortcode, the difference calculation assumes you want to calculate the difference between the start time and the current time when the page is visited. If I understand correctly, you want the difference between the two custom field values, not the difference from start to now. So you should add the corresponding Types field shortcode in the "end" attribute of the custom shortcode, as you see the start time shortcode in the "start" attribute now.
Displaying just the hour:mins is what I want. But I thought that I did put the start & end fields in the function? Isn't that where the calculation happens?
Parse error: syntax error, unexpected ';', expecting ')' in /home/s3361/html/wp-content/themes/Divi-child/functions.php on line 13
Notice: Undefined property: wpdb::$actionscheduler_actions in /home/s3361/html/wp-includes/wp-db.php on line 648
WordPress database error: [Table 's3361.a' doesn't exist]
SELECT a.action_id FROM a WHERE 1=1 AND a.hook='wp_mail_smtp_send_usage_data' AND a.status='pending' ORDER BY a.scheduled_date_gmt ASC LIMIT 0, 1000
Notice: Undefined property: wpdb::$actionscheduler_actions in /home/s3361/html/wp-includes/wp-db.php on line 648
WordPress database error: [Table 's3361.a' doesn't exist]
SELECT a.action_id FROM a WHERE 1=1 AND a.hook='wp_mail_smtp_send_usage_data' AND a.status='pending' ORDER BY a.scheduled_date_gmt ASC LIMIT 0, 1000
It doesn't appear to be calculating properly. If you look at the screen shot, the the second to last column is the correct hours calculated and the last column uses the function.