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);
}
}
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:
enlace oculto
Returns the difference between two DateTime objects
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);
}
}
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
Yes, that works! You wouldn't believe how many ways I tried to achieve this so thank you for getting me there 🙂