Skip Navigation

[Resolved] Modify date upon save

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

Problem:
How to modify (add one day as example) the Types Date field upon CRED Form submission.

Solution:
It requires a CRED Custom Code, along with strtotime()

Please check this answer: https://toolset.com/forums/topic/modify-date-upon-save/#post-374533
It contains a sample code that will add one day to the chosen date when the CRED is submitted

Relevant Documentation:
http://php.net/manual/de/function.strtotime.php

This support ticket is created 8 years, 1 month 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Tagged: 

This topic contains 7 replies, has 2 voices.

Last updated by gavinS 8 years, 1 month ago.

Assisted by: Beda.

Author
Posts
#373665

I have noticed a problem with the way dates are filtered in views.

I am trying to sum the values for a field 'potential-value' in the type 'Job'. I want to sum the values within a calendar month. (ie from the 1st of this month to the last day if this month).

However, if I use the filter:

Select posts with custom field:
Start Date is a number between THIS_MONTH(), FUTURE_MONTH(1)

Then it includes the values from the 1st of this month to the 1st of next month. (Ie. if I have a job with the start date of the 1st April then it is added to the sum. This is a problem for me.

However, the exact date is not as important to me as the month, so I thought perhaps I could use cred_save_data, so when the form is saved, if the date is the 1st of the month, then change it to the 2nd of the month. Then at least I would get the right values in the sums, even though the date is slightly off.

I'm not really sure how to change the date like this? Any help would be appreciated.

#373673

Tried this, but it's not working so guess I did something wrong?

add_action('cred_before_save_data', 'first_to_second',10,1);
function first_to_second ($form_data)
{
    // if a specific form
    if ($form_data['id']==140 || $form_data['id']==152)
    {
        if (isset($_POST['my_custom_field']))
        {
           $entered_date = $_POST['wpcf-start-date'];
           date_modify($entered_date,'+1 day');
           $_POST['wpcf-start-date'] = $entered_date;
        }
        
    }
}
#373698

In my opinion date_modify() does manipulate Weekdays and not Timestamp Dates.
hidden link

Do you agree?

I would rather try this:

strtotime('+1 day', $timestamp);

There are different possibilities but I think that is the best one.
http://stackoverflow.com/questions/2515047/how-do-i-add-24-hours-to-a-unix-timestamp-in-php
hidden link

Please let me know if the above solution works for you, I look forward to your reply!

Thank you for your patience.

#374215

Hi Beda

I tried that as follows:

add_action('cred_before_save_data', 'first_to_second',10,1);
function first_to_second ($form_data)
{
    // if a specific form
    if ($form_data['id']==140 || $form_data['id']==152)
    {
        if (isset($_POST['my_custom_field']))
        {
           $entered_date = $_POST['wpcf-start-date'];
           strtotime('+1 day', $entered_date);
           $_POST['wpcf-start-date'] = $entered_date;
        }
        
    }
}

But still no change to the start date. Is there something wrong with the way I'm trying to change it that you can see?

#374307

Why do you keep the if (isset($_POST['my_custom_field'])) in your code?

Do you have a field named my_custom_field and need to check if it exists?

The Code on the API page is dummy example code.

Also I am not sure why you use cred_before_save_data

This should be pretty much possible after you saved the data as well.

Let me put a example below how to use it in your case:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==12)
    {
    	//we get stored Date
        $entered_date = get_post_meta($post->ID, 'wpcf-start-date', true);

        //We add a Day
        $new_date = strtotime('+1 day', $entered_date);
        
        // add it back to saved post meta
        update_post_meta($post->ID, 'wpcf-start-date', $new_date);
        
    }
}

Working?

#374321

Bwahahaha. Can't believe I left the 'my_custom_field' in there.

Anyway I've tried this and still doesn't seem to be changing the date??

add_action('cred_save_data', 'first_to_second',10,2);
function first_to_second($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==140) 
    {
        //we get stored Date
        $entered_date = get_post_meta($post->ID, 'wpcf-start-date', true);
 
        //We add a Day
        $new_date = strtotime('+1 day', $entered_date);
         
        // add it back to saved post meta
        update_post_meta($post->ID, 'wpcf-start-date', $new_date);
         
    }
}

Can you send private login option. Maybe you can see something I can't?

I also see there seems to be a debug popup coming up on the cred form page when I submit, somebody must have left it active. How do I stop this?

#374533

I'm so sorry man - don't know what has ridden me,

Of course using the CRED API we access the current edited/created Post with $post_id and not with
$post->ID

So (tested!) just change the code accordingly and it will work:

add_action('cred_save_data', 'first_to_second',10,2);
function first_to_second($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==140) 
    {
        //we get stored Date
        $entered_date = get_post_meta($post_id, 'wpcf-start-date', true);
  
        //We add a Day
        $new_date = strtotime('+1 day', $entered_date);
          
        // add it back to saved post meta
        update_post_meta($post_id, 'wpcf-start-date', $new_date);
          
    }
}

Please let me know if the above solution works for you, I look forward to your reply!

Thank you for your patience.

#374545

Thanks Beda

Working perfectly. <does a little dance>

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