A
A
agapofff2021-05-14 09:51:16
Design
agapofff, 2021-05-14 09:51:16

PHP recalculation of paid time when switching to another tariff: what am I doing wrong?

Dear experts, I ask for help, because my head is spinning.

There is a service with a subscription, which has several tariffs.

It is necessary to give the user the opportunity to switch to any tariff at any time, - accordingly, by recalculating the paid time from the current tariff to the new one.

The calculation is simple:

$secondsInMonth = 60 * 60 * 24 * 30;                                // количество секунд в периоде оплаты (30 дней)
$paidBefore = strtotime('2021-05-30 23:50:00');                     // дата окончания подписки
$now = strtotime('now');                                            // текущее время
$currentTariffPrice = 1590;                                         // цена текущего тарифа (за 30 дней)
$newTariffPrice = 7990;                                             // цена нового тарифа (за 30 дней)

$currentTariffPriceBySecond = $currentTariffPrice / $secondsInMonth;// получаем цену текущего тарифа за секунду, разделив цену тарифа на количество секунд
$newTariffPriceBySecond = $newTariffPrice / $secondsInMonth;        // аналогично получаем цену нового тарифа за секунду

$secondsRemain = $paidBefore - $now;                                // получаем оставшееся время, вычитая текущее время из даты окончания подписки
$secondsPaid = $secondsRemain * $currentTariffPriceBySecond;        // получаем остаток в деньгах по текущему тарифу, умножив оставшееся количество секунд на цену за секунду

$newTariffSecondsRemain = $secondsPaid / $newTariffPriceBySecond;   // получаем оставшееся количество секунд по цене нового тарифа

$newTariffPaidBefore = $now + $newTariffSecondsRemain;              // получаем дату окончания подписки по новому тарифу, добавляя к текущему времени оставшееся количество секунд по новому тарифу

return date('Y-m-d H:i:s', $newTariffPaidBefore);                   // выводим


As if, the recalculation at the time of the request is correct, - however, the result becomes larger on the next request.
That is, we consider - we receive date.
We wait 10 minutes, count again, - we get the date 10 minutes more.

But, if the logic does not change me, since the remaining time at the current tariff has naturally decreased by the past, say, 10 minutes, then the balance in money will decrease, and, therefore, the remaining time at the new tariff will proportionally decrease, - and the end date subscriptions under the new tariff should remain unchanged over time.

What didn't I learn?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
K
Konstantin Davydov, 2018-11-18
@space_pancake

Flat in isometric

S
Sanes, 2018-11-18
@Sanes

Flat

X
xmoonlight, 2018-11-18
@xmoonlight

This is not design.
This is a poster for a publication about web layout or web usability.

N
nazgulun, 2021-05-14
@nazgulun

Why is the date not static? Because it can be static only when the second tariff is on, and you spend money on the first one while you wait - tritite less than you would spend on the second one.
That is, by the end of the waiting period, you have more money left than if you were waiting with the second tariff turned on - that is, when switching, the date will be longer.
If the analogy is simpler - as if you suspended your home Internet for a vacation for 50 rubles a month - time has passed and the end date has increased because less money has been spent.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question