简体   繁体   中英

How to calculate a range of a date php of a billing cycle?

I want to check if a timestamp is in a range of the billing cycle of a user in my web site. For example

If my user was registered in 03/07/2012. I would like to know if he has made more than three offers in a month of his billing cycle. for example if one day(04/09/2013) he tries to make an offer I have to finde his range.

I have stored a timestamp with his register time in mysql database. I was trying to use mktime but I cant manage the function

If I got you right, you know when user has registered and if now (or at any other date) he makes some action, you need to know the dates of beginning and end of billing month?

Like, if I have registered on 2012-02-04 , so at the moment my billing month would be 2013-07-04 - 2013-08-04 ?

Take a look at this:

$registerDate = '2012-05-04';
$date = '2013-04-15';

$from = strtotime($registerDate);
$to = strtotime($date);

$interval = date_diff(date_create(date('Y-m-d H:i:s', $from)), date_create(date('Y-m-d H:i:s', $to)));

$billingMonthStart = strtotime($interval->format('-%d days'), $to);
$billingMonthEnd = strtotime('+1 month', $billingMonthStart);

So, now if I run

var_dump(@date('Y-m-d', $billingMonthStart), @date('Y-m-d', $billingMonthEnd));

I will get such result:

string(10) "2013-04-04"
string(10) "2013-05-04"

Is that what you have meant?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM