开发者

php date/time intervals with subscription

I need some help wrapping my head around this. I am trying to develop something that does monthly, weekly, quarterly, yearly subscriptions. I need to figure out when the next charge date is. But I'm getting confused getting through the logic. I can easily calculate when a year is from the original transaction (<? echo date("Y-m-d",strtotime($charge_interval, strtotime($original_charge_time))); ?>), where $charge_interval is '+3 months' or '+1 month', etc. But.. if 3 months have passed, I can't just use that equation, because it will 开发者_JAVA技巧show the "next" date as one month from the original. Any advice?

EDIT: more precision. My data is stored in a DB, and I need to ensure its getting the "next" month rather than a month from original date. Say transaction took place Nov 15th. Its now July 6th. I want "next transaction" to say July 15th, NOT Dec 15th (which is what the above code would accomplish), and to say "we have already have 7 charges before".


strtotime is little-know, but can show itself to be very useful for this kind of use.

EDIT: here may be a clue:

$charges = 0;
$now = time();
$next_charge_time = strtotime($original_charge_time);
while ($next_charge_time < $now)
{
    $charges++;
    $next_charge_time = strtotime($charge_interval, $next_charge_time);
}
$next_charge_time = date("m/d/Y", $next_charge_time);
echo "You have been charged $charges times since $original_charge_time.";
echo "<br/>";
echo "Next charge will be on $next_charge_time";


Doesn't php have a dateadd function of some kind to add a number of months to a date?

Like this one?

http://php.net/manual/en/function.date-add.php


Use DateTime() with http://us3.php.net/manual/en/datetime.add.php


Try mktime(0, 0, 0, strftime("%d", $origtime), date("m")+1, date("Y"));

that would mean midnight of course (first three parms are H, M, S, you could use strftime for those too if you want)

that would work for the next calendar month, you could just remove the "+1" for the same month if you work out the day falls later than the current day.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜