In PHP, how do I add 3 months to the purchase date retrieved from the iPhone InApp receipt?

冷暖自知 提交于 2019-12-22 13:34:25

问题


After I have validated the receipt against the App Store from my PHP server, the App Store sends me back a JSON response with

"status" : 0 
"receipt" : ( .... )

One of the receipt items is "purchase_date" which contains the following string (example) "2010-02-09 19:17:04 Etc/GMT"

I'm trying to establish a subscription service and would like to add 3 months to this date and then write that expiry date into a MySQL table.

Is there a string-to-date type function in PHP that can allow me to achieve the adding of 3 months?

I have found this example which looks like it adds 1 month to a date:

$date = date("Y-m-d");// current date
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");

But I'm not sure how I can turn the string passed by the App Store into a PHP recognised date.


回答1:


You have already done it, you just added one step too many.

$purchase_date = "2010-02-09 19:17:04";
$purchase_date_timestamp = strtotime($purchase_date);
$purchase_date_3months = strtotime("+3 months", $purchase_date_timestamp);

echo "Purchase date + 3months = ".date("Y-m-d h:i:s", $purchase_date_3months);

this depends on strtotime recoginzing your time string - try it out first. But usually, strtotime can recognize any properly formatted anglo-american date/time string. If in doubt, check the manual.




回答2:


Don't treat time as a scalar value - It's much more complex than that. I would suggest that you push this logic into your database, provided that you use a one for storing data? Alternatively, recent versions of PHP has a datetime object, which can be used.



来源:https://stackoverflow.com/questions/2305902/in-php-how-do-i-add-3-months-to-the-purchase-date-retrieved-from-the-iphone-ina

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!