PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Start Date / End Date - calculate # of days - HOW?

  1. studio57designs
    Member

    I have a rental form in Cart66 that has a start date and an end date. Is it possible to enter both start and end date and then calculate the # of days?

    For example, the customer enters the start date of 5/1/2012 and end date of 5/6/2012 and the daily rate is $6.00 - we'd like to see the form pass the amount as $30 (5 days x $6) to the cart.

    Is there a way to create a function to do that or is it already built in and I've just not found it?

    Thanks,

    Louise

    Posted 12 years ago on Saturday April 28, 2012 | Permalink
  2. studio57designs
    Member

    Is this even possible? Anyone?

    Louise

    Posted 12 years ago on Monday April 30, 2012 | Permalink
  3. David Peralty

    I just wanted to let you know that I've asked one of the developers to take a look at this post to see if they can give you any guidance. We aren't ignoring you. :)

    Posted 12 years ago on Monday April 30, 2012 | Permalink
  4. studio57designs
    Member

    Ok, thanks so much! I'll wait to hear from your guys.

    Louise

    Posted 12 years ago on Monday April 30, 2012 | Permalink
  5. Hello Louise,
    I am not familiar with the details on how Cart66 integrates with Gravity Forms, so I can't give you a complete code snippet. However, you can use one of our hooks to calculate the number of days between two dates, come up with a total price based on a daily rate and finally store that result into another Gravity Forms field. Hopefully this will help you achieve your goal. If you still aren't able to make this work, I would suggest reaching out to the Cart66 folks. as they are the ones who built the integration with Gravity Forms and should be more familiar with it.

    add_action("gform_pre_submission", "pre_submission_handler");
    function pre_submission_handler($form){
    
        $start_date = $_POST["input_2"]; //NOTE: replace 2 with your actual start date input ID
        $end_date = $_POST["input_3"];  //NOTE: replace 3 with your actual end date input ID
        $days = gf_date_diff($start_date, $end_date);
    
        $daily_rate = 6.00;
    
        $_POST["input_4"] = floatval($daily_rate) * floatval($days); //NOTE: replace 4 with your actual total field ID (field that will be passed to Cart66)
    }
    
    function gf_date_diff($start, $end) {
        $start_ts = strtotime($start);
        $end_ts = strtotime($end);
        $diff = $end_ts - $start_ts;
        return round($diff / 86400);
    }

    Cheers,
    Alex

    Posted 12 years ago on Monday April 30, 2012 | Permalink
  6. studio57designs
    Member

    Thank you Alex. I gave it a try, but did not work for me. It was passing a value of 1 instead of the actual # of days.

    I'll check with Cart66....but, I really appreciate the response. Gives me some place to start.

    Louise

    Posted 12 years ago on Tuesday May 1, 2012 | Permalink