Jump to content

Date Calculation


ainoy31

Recommended Posts

Hello-

 

I am having trouble trying to calculate a billing cycle on a monthly basis.  Say the billing cycle start date is 2008-2-15, then the next cycle should start at 2008-03-14.  Here is my script that I have tried.

 

echo $Date = '2008-02-15';

$numDays = (date('t', $currentDate = strtotime("$Date")));

echo "A month from now: " . (date('Y-m-d', mktime(0,0,0,date('d', $currentDate)+ $numDays,0,0)));

 

Much appreciation.  AM

Link to comment
https://forums.phpfreaks.com/topic/113148-date-calculation/
Share on other sites

I am trying to take into account that there is never 30 days in a bill cycle since the days in the months are different.  So, I figure how many days are in the month that the bill cycle starts and then add the total days to get to the next start of the bill cycle.  Hope that is not confusing.

Link to comment
https://forums.phpfreaks.com/topic/113148-date-calculation/#findComment-581304
Share on other sites

Do you mean that you just want to add the total number of days in the relevant month to the respective days?

 

Would that mean that if the cycle starts on January 31st, the next one would be March 3rd (if it isn't a leap year)? Because 31 days after that day is more than the number of days in February. Actually, every 31 day month that is followed by a 30 day month would go to the first of two months later. Did you mean something else, or is that how you want it to work?

Link to comment
https://forums.phpfreaks.com/topic/113148-date-calculation/#findComment-581318
Share on other sites

// Exactly 30 days in the future:

date("Y-m-d", mktime(0,0,0,date("m"), date("d")+30, date("Y")));

 

// Exactly 1 month in the future:

date("Y-m-d", mktime(0,0,0,date("m")+1, date("d"), date("Y")));

 

// Date in future based on how many days are in the current month:

date("Y-m-d", mktime(0,0,0,date("m"), date("d")+cal_days_in_month(CAL_GREGORIAN, date("m"), date("Y")), date("Y")));

Link to comment
https://forums.phpfreaks.com/topic/113148-date-calculation/#findComment-581325
Share on other sites

Using the strtotime() function is even easier:

<?php
//Exactly 30 days in the future
echo date('Y-m-d',strtotime("+30 days")) . '<br>';
//Exactly 1 month in the future
echo date('Y-m-d',strtotime('+1 month')) . '<br>';
// Date in future based on how many days are in the current month:
echo date('Y-m-d',strtotime('+' . date('t') . ' days'));
?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/113148-date-calculation/#findComment-581348
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.