Answer the question
In order to leave comments, you need to log in
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); // выводим
Answer the question
In order to leave comments, you need to log in
This is not design.
This is a poster for a publication about web layout or web usability.
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 questionAsk a Question
731 491 924 answers to any question