Jump to content

Date Function Issue


ainoy31

Recommended Posts

Hello-

 

I need help with my date function.  Basicly I am going back to three previous billing cycles for a client from the current date.  I figure out when their bill start date is and then figure out the total days in the month to give me when it starts and ends.  My problem is calculating the previous bill cycle end date.  Here is my code.

 

for($x = 3; $x > 0; $x--)
{
$previousThreeBillCycle = mktime(0, 0, 0, date("m") - $x, $billDay, date("Y"));
echo $startBillCycle = date("Y-m-d", $previousThreeBillCycle);  //this prints 2008-05-01
list($year, $month, $day) = explode('-', $startBillCycle);
echo "<br>";
echo $daysInMonth = date("t", mktime(0,0,0, $month, 1, $year)); //31 days in this month
echo "<br>";
echo $endBillCycle = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d", $startBillCycle) + $daysInMonth, date("Y")));  //this where I am helping an issue
echo "<br>";
}

 

Hope this is not confusing to follow.  Much appreciation.  AM

 

 

Link to comment
https://forums.phpfreaks.com/topic/119149-date-function-issue/
Share on other sites

the end bill cycle should be the start bill cycle plus the days in the month such as if the start bill cycle is 2008-05-01 then the end bill cycle is 2008-06-01.

 

Is this also the case for February? If so, you have a shorter billing cycle for one customer than another. Now, on the other hand, if the rule is simply that the billing cycle is the same DAY of the month every time, it is extremely easy to calculate (except you need to account for the months with fewer days). So, let's assume you sign up on the 31st of January, when it comes to April, does your bill fall on April 30 (last day of the  month), or does it fall on May 1 (31 days after April 1)?

 

Here is how I would recommend finding out when the billing cycles would end:

<?php
$signupdate = '2007-01-31'; // January 31, 2007
list ($y, $m, $d) = explode('-', $signupdate);
// Let's get all the billing dates for last year
while ($m < 13)
{
  // See what the last day of the month is
  $lastday = date('t', mktime(0, 0, 0, $m, 1, $y));

  // If the last day of the month is less than the current day, use the last day instead
  if ($lastday < $d)
  {
    $billtime = mktime(0, 0, 0, $m, $lastday, $y);
  }··
  else
  {
    $billtime = mktime(0, 0, 0, $m, $d, $y);
  }

  // Print out this bill cycle
  echo date('M j, Y', $billtime) . "<br />\n";

  // Increment to the next month
  $m++;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/119149-date-function-issue/#findComment-613544
Share on other sites

No, the bill start day can be different for each client.  Someone can have it start on the 10th of the month.  That is why in the code:

 

$previousThreeBillCycle = mktime(0, 0, 0, date("m") - $x, $billDay, date("Y"));

 

$billDay is derived from a call to another method, getBillDay.  Also, that is why I am calculating how many days are in each month since the total days are different. 

$daysInMonth = date("t", mktime(0,0,0, $month, 1, $year));

Link to comment
https://forums.phpfreaks.com/topic/119149-date-function-issue/#findComment-613566
Share on other sites

Ok.  seems like this is working.

 

$billDay = $objCustomer->getCustomerBillDay(); 
//for this example, the $billDay = 10 but it is dynamic not static

for($x = 3; $x > 0; $x--)
{
$previousThreeBillCycle = mktime(0, 0, 0, date("m") - $x, $billDay, date("Y"));
echo $startBillCycle = date("Y-m-d", $previousThreeBillCycle);  
list($year, $month, $day) = explode('-', $startBillCycle);
echo "<br>";
echo $daysInMonth = date("t", mktime(0,0,0, $month, 1, $year));
echo "<br>";
$endBillCycle = strtotime("+1 month", $previousThreeBillCycle);
echo date("Y-m-d", $endBillCycle);
echo "<br>";
echo "<br>";
}

 

the result is:

2008-05-10

31

2008-06-10

 

2008-06-10

30

2008-07-10

 

2008-07-10

31

2008-08-10

 

My question is why is the endBillCycle is not calculating right?  For example, the start date is 2008-05-10 and the end date should be 2008-06-09 not 2008-06-10 as above.  Thanks.  AM

 

Link to comment
https://forums.phpfreaks.com/topic/119149-date-function-issue/#findComment-613574
Share on other sites

No, the bill start day can be different for each client.  Someone can have it start on the 10th of the month.  That is why in the code:

 

I understand that, but I'm trying to show you that there is a difference between calculating based on day of the month and a true number of days. If you calculate solely based on the number of days in the month in which the client signs up, you are going to have people who have different length cycles. For instance, if you sign up someone in February, their cycle is only 28 days (unless they sign on leap year, and then it is 29); however, if someone signs up in January, they get 31 days per cycle. It's not a good business practice to have people with different length cycles for the same type of membership.

 

On the other hand, if you simply define the fact that ever cycle is 30 days, you can easily calculate the billing cycles:

<?php
$length = 30; // in days
$signup = '2007-01-15';

list($y, $m, $d) = explode('-', $signup);

// Next 10 billing cycles
for ($i = 1; $i < 11; $i++)
{
  $cycle = mktime(0, 0, 0, $m, $d + ($i * $length), $y);
  echo date('m/d/Y', $cycle) . "<br />\n";;
}
?>

 

Also, I'm not posting exact code to solve your problem, but I am trying to give you a concept that you can apply to your own code and fix your problem.

Link to comment
https://forums.phpfreaks.com/topic/119149-date-function-issue/#findComment-613624
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.