gpt4 book ai didi

ruby-on-rails - 如何计算订阅计划的下一个续订日期?

转载 作者:数据小太阳 更新时间:2023-10-29 07:46:12 26 4
gpt4 key购买 nike

我设置了一个订阅计划,每月、每三个月和每年都会自动向用户收费。问题是,我如何计算向用户收费的确切日期?我们不能每 30 天充电一次吗?因为并非所有月份都有 30 天。如果我们在购买日期收费,那么在 1 月 30 日购买计划的人将不会在 2 月被收费,因为 2 月没有 30。这是一个有点复杂的问题。我该如何解决这个问题?

PS:我在 rails 上使用 ruby​​

最佳答案

您可以存储 subscribed_on 日期和用户已支付的 number_of_payments。然后通过增加 number_of_months 并在 date 上使用 >>> 方法来计算下一个付款日期。 >>> 考虑一个月中的天数:

# at subscription date and first payment
subscribed_on = Date.today # in this example: 2015-01-30
number_of_payments = 1
next_payment_date = subscribed_on >> number_of_payments
#=> 2015-02-28

# after next payment (on 2015-02-28)
number_of_payments = number_of_payments + 1
next_payment_date = subscribed_on >> number_of_payments
#=> 2015-03-30

# after next payment (on 2015-03-30)
number_of_payments = number_of_payments + 1
next_payment_date = subscribed_on >> number_of_payments
#=> 2015-04-30

来自 Date#>> 的文档

d >> ndate

Returns a date object pointing n months after self. The n should be a numeric value.

这不仅返回正确的日期,还允许优化查询 next_payment_date 和统计信息,例如以月为单位的订阅时间的平均长度。

关于ruby-on-rails - 如何计算订阅计划的下一个续订日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29729404/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com