PHP generates an incorrect next payment date from December 29

I use the below logic in PHP to generate the following billing date based on the pricing for my subscription:

// calculating next payment date
$today = date('d');
$checkDate = '15';
if ($today >= $checkDate) {
    // subscription purchase after 15.
    $nextPaymentDate = date('Y-m-01', strtotime('+2 month'));
} else {
    // subscription purchase before 15.
    $nextPaymentDate = date('Y-m-01', strtotime('next month'));
}

Basically, when a customer purchases a subscription before date 15, the next payment date should be set to the first date of the next month. In addition, it should be set on the first date of the next month.

This logic worked until yesterday (until December 28, 2014) . As expected until December 15, I received 2015-01-01. And after December 15th I received 2015-02-01. But from today, I get nextPaymentDate as 2015-03-01.

I also tried

// ....
if ($today >= $checkDate) {
    // subscription purchase after 15.
    $nextPaymentDate = date('Y-m-01', strtotime('next month'));
    $nextPaymentDate = date($nextPaymentDate, strtotime('next month'));
// ....

But it gives the same result: (

, , 29 30 31 . , 31- .

? , .

+4
1

, :

date("Y-m-01", strtotime(date("Y-m-01")." +2 months"));
+4

Source: https://habr.com/ru/post/1569304/


All Articles