Skip Navigation

[Resolved] Populate custom field with number of days calculated from two date fields

This thread is resolved. Here is a description of the problem and solution.

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 5 years, 7 months ago. 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.

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)

This topic contains 4 replies, has 2 voices.

Last updated by julieP 5 years, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#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:
hidden 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 🙂

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.