0

I am working with WooCommerce Subscriptions and am trying to set it so that users can select their own start date using another plugin. Within the date picker plugin, if a user chooses a date within the middle of the month, the payments are supposed to be prorated and then the subscription starts on the first day of the next month. However, the plugin works to only start at the day the subscription starts. I am trying to adjust this so it chooses the start date of the first day of the next month. I have used this as reference - How to find first day of the next month and remaining days till this date with PHP

function woosubscriptions_custom_cart_next_payment_date( $start_pay_date , $recurring_cart){
                foreach($recurring_cart->get_cart() as $cart_item_key => $cart_item){
                    if(isset($cart_item['start-subcription']) && !empty($cart_item['start-subcription'])){
                        $days_free_trial = WC_Subscriptions_Product::get_trial_length( $cart_item['data'] );
                        $start_subcription = $cart_item['start-subcription'];
                        if($days_free_trial > 0){
                            $start_subcription .= ' ' . $days_free_trial . ' days';
                        }
                        $offset=5*60*60; //converting 5 hours to seconds.
                        $start_pay_date = gmdate( 'Y-m-d H:i:s', strtotime($start_subcription) + $offset);
                        $first_day_next_month = ($start_pay_date, strtotime('first day of next month'));
                        break;
            }
        }
        return $first_day_next_month;
    }
    add_filter( 'wcs_recurring_cart_next_payment_date', 'woosubscriptions_custom_cart_next_payment_date', 10 , 2);

It is not correctly getting the first day of next month.

kayzoe
  • 61
  • 10
  • 1
    `$first_day_next_month = ($start_pay_date, strtotime('first day of next month'));` doesn't look right. Is `$start_pay_date` meant to be used as the second parameter for your `strtotime` call? – ceejayoz Oct 06 '20 at 12:19
  • 1
    Why not use `DateTime`?. You could write something like this: $date = new DateTime('2020-10-07'); $date->modify('first day of this month'); echo $date->format('jS, F Y');. You could feed the selected day to the DateTime object in your function. – MaD Oct 06 '20 at 12:24
  • @ceejayoz - Yes, thst's right. That was the plugin's initial code, and I am trying to grab the first day of next month from it. – kayzoe Oct 06 '20 at 12:25
  • ...if that does not help yet, please highlight whatever is missing. Also, please explain how this question is related to WooCommerce. If you are only searching for a calculation for such a date, WooCommerce does not look related to me – Nico Haase Oct 06 '20 at 12:40

1 Answers1

2

Your code to calculate the first day of next month is not quite right. The answer you have linked looks different to the code you have used.

strtotime('first day of next month') calculates the first day of the current month and will bear no relation to the start date.

You could use a DateTime object and figure it out from there:

$start_pay_date = new DateTime($start_subcription);
$first_day_next_month = $start_pay_date->modify("first day of next month");

$first_day_next_month would then represent the first day of the month after your $start_pay_date, you can use any of the DateTime methods to then format it how you want (e.g. $first_day_next_month->format("Y-m-d"))

Dharman
  • 30,962
  • 25
  • 85
  • 135
Luke
  • 1,060
  • 8
  • 19