Skip Navigation

[Gelöst] Populate custom field with number of days calculated from two date fields

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem:

I have 3 custom fields: wpcf-start-date, wpcf-end-date and wpcf-total-days. When a form is submitted I'd like wpcf-total-days to be populated with the number of days from the other two fields.

Solution:

It needs custom codes, see the solution in details:
https://toolset.com/forums/topic/populate-custom-field-with-number-of-days-calculated-from-two-date-fields/#post-1109329

Relevant Documentation:

http://php.net/manual/en/datetime.diff.php

This support ticket is created vor 6 Jahren, 2 Monaten. 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.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Dieses Thema enthält 4 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von julieP vor 6 Jahren, 2 Monaten.

Assistiert von: Luo Yang.

Author
Artikel
#1109258

I have 3 custom fields: wpcf-start-date, wpcf-end-date and wpcf-total-days. When a form is submitted I'd like wpcf-total-days to be populated with the number of days from the other two fields. I've created this hook but the number returned is '1'. Not sure where it's not quite right! Can you help please?

add_action('cred_submit_complete', 'after_save_data_action_111',10,2);
function after_save_data_action_111($post_id, $form_data) {
    
    // if a specific form
    if ($form_data['id']==111) {
   
   	$date1 = get_post_meta($post->ID, 'wpcf-start-date', true);
	$date2 = get_post_meta($post->ID, 'wpcf-end-date', true);
   $secondsdiff = $date1 - $date2 - 86400;
   $totaldays = ( $secondsdiff / ( 60*60*24*-1) );
   update_post_meta($post_id, 'wpcf-total-days', $totaldays);
}
}
#1109329

Hello,

I suggest you try the PHP function date_diff(), for example, you can modify your PHP codes as below:

add_action('cred_submit_complete', 'after_save_data_action_111',10,2);
function after_save_data_action_111($post_id, $form_data) {
    // if a specific form
    if ($form_data['id']==111) {
		$date1 = get_post_meta($post_id, 'wpcf-start-date', true);
		$date1_obj = new DateTime();
		$date1_obj->setTimestamp($date1);
		
		$date2 = get_post_meta($post_id, 'wpcf-end-date', true);
		$date2_obj = new DateTime();
		$date2_obj->setTimestamp($date2);
		
		$interval = $date1_obj->diff($date2_obj);
		$totaldays = $interval->format('%a'); //here apply your custom fomular 
		update_post_meta($post_id, 'wpcf-total-days', $totaldays);
	}
}

And test again, more help:
versteckter Link
Returns the difference between two DateTime objects

#1109411

Hi Luo

$totaldays = $interval->format('%a'); //here apply your custom fomular

Sorry but isn't a format expected here not a formula so '%d'??

If I have dates 20th Sept start and 24th Sept end, I get the number 4 when I use this:-

add_action('cred_submit_complete', 'after_save_data_action_111',10,2);
function after_save_data_action_111($post_id, $form_data) {

    // if a specific form
    if ($form_data['id']==111) {
	
        $date1 = get_post_meta($post_id, 'wpcf-start-date', true);
        $date1_obj = new DateTime();
        $date1_obj->setTimestamp($date1);
         
        $date2 = get_post_meta($post_id, 'wpcf-end-date', true);
        $date2_obj = new DateTime();
	$date2_obj->setTimestamp($date2);
         
        $interval = $date1_obj->diff($date2_obj);
        $totaldays = $interval->format('%d');
        update_post_meta($post_id, 'wpcf-total-days', $totaldays);
    }
}

I need to add 86400 seconds (1 day) to date2 so the calculation is correct and tried this but still got 4:-

add_action('cred_submit_complete', 'after_save_data_action_111',10,2);
function after_save_data_action_111($post_id, $form_data) {

    // if a specific form
    if ($form_data['id']==111) {
	
        $date1 = get_post_meta($post_id, 'wpcf-start-date', true);
        $date1_obj = new DateTime();
        $date1_obj->setTimestamp($date1);
         
        $date2 = get_post_meta($post_id, 'wpcf-end-date', true);
        $date2_obj = new DateTime();
	$date2_obj->add(new DateInterval('PT86400S'));
	$date2_obj->setTimestamp($date2);
         
        $interval = $date1_obj->diff($date2_obj);
        $totaldays = $interval->format('%d');
        update_post_meta($post_id, 'wpcf-total-days', $totaldays);
    }
}
#1109783

I assume you are going to add 1 day to the result, you can try to modify this line from:

$totaldays = $interval->format('%a'); //here apply your custom formula

to:

$totaldays = $interval->format('%a') + 1; //here apply your custom formula
#1110257

Yes, that works! You wouldn't believe how many ways I tried to achieve this so thank you for getting me there 🙂