Link to home
Start Free TrialLog in
Avatar of slickip
slickip

asked on

PHP and Month February headache

Hi again.

I got another little headache, again.

I have the following code:


$dstart = '2011-12-30';
$dstart = strtotime($dstart);
$swmlength = 4;

for ( $counter = 1; $counter <= $swmlength; $counter += 1) {

$enddate = date('Y-m-d',strtotime(+$counter." month", $dstart));
$startdate = date('Y-m-d',strtotime("-1 month",strtotime($enddate)));
$enddate = date('Y-m-d',strtotime("+1 month -1day",strtotime($startdate)));
echo $startdate.' ---- '.$enddate.'<br />';
}

Open in new window


This will output the following. As one can see, the Month March is very wrong:
2011-12-30 ---- 2012-01-29
2012-02-01 ---- 2012-02-29
2012-03-01 ---- 2012-03-31
2012-03-30 ---- 2012-04-29

Open in new window


I would like it to be:
2011-12-30 ---- 2012-01-29
2012-02-01 ---- 2012-02-29
2012-03-01 ---- 2012-03-31
2012-04-01 ---- 2012-04-30

Open in new window


This looks similar to an previous question, but its actually a different project.

Again, my hat off to anyone with a solution. Its greatly appreciated.

Best Regards.



Avatar of xterm
xterm

Is your logic the following?
1) For the first month displayed, the range is always $dstart -> 3rd last calendar day of month afterwards
2) For all subsequent months displayed, the range is always first day of calendar month after the month that is the 2nd part of #1 above, to last day of the calendar month that is 2nd part of #1 above?

I'm just not sure what you're trying to do here - your "wish for" output doesn't follow any meaningful logic.

SOLUTION
Avatar of john-formby
john-formby
Flag of Ghana image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of slickip

ASKER

Hi xterm!

Thanks for the response. Well basically what I am trying to get is the following:

I have a $dstart which starts on the 2011-12-30. The $swmlength is 4 month in my example. I want to create entries that show me the next 4 month including beginning month, which is set in the $dstart variable.


So, we start or first month with:
2011-12-30 till 2012-01-30

Open in new window

2nd month to be
2012-01-31 till 2012-02-29

Open in new window

3rd month
2012-03-01 till 2012-03-30

Open in new window

etc.

This also is the reason why I added this line in:
$enddate = date('Y-m-d',strtotime("+1 month -1day",strtotime($startdate)));

Open in new window

Without this line, my result would become:
 
2011-12-30 till 2012-01-30
2012-01-30 till 2012-02-28


With my currently code, which is very wrong, I get:
2011-12-30 ---- 2012-01-29
2012-02-01 ---- 2012-02-29
2012-03-01 ---- 2012-03-31
2012-03-30 ---- 2012-04-29

Open in new window


I hope this explains all a little but more. Please feel free ask if unsure.

Thank you!
Avatar of slickip

ASKER

Hi john-formby,

Your code produces date ranges, starting from 1st of the month till the last day of the month. It doesn't give me the date range that I am after. I wasn't totally clear in my first post about what I am trying to do, I am sorry.

I have tried explaining my goal in a reply to xterm. Could you please look at it and see if this makes more sense?

Sorry for the inconvenience.

Best Regards.
Hi slickip,

I am still totally confused with this one to be honest :-S

You start with a date somewhere in a month.  Then your logic seems to be that you echo out:

1. The initial starting date ---- Two days before the end of the following month
2. The start date of the following month ---- The last date of that month
3. The start date of the following month ---- The last date of that month
4. One day less than the end of the previous month ---- One days before the end of the following month

I really do not understand the rules for this as there does not appear to be a pattern.  Please can you provide more details behind the logic?

Kind Regards,

John
Avatar of slickip

ASKER

Hi john-formby,

Sorry for causing so much confusion. Sometimes, well for me at least, its hard to explain something in words. I will try again.

I have a fitness membership database that basically starts someones membership at the time he joins. That’s explains the unusual starting date. With each membership, depending on the duration that the member choses, we include swim access to our pool. The swim access allow for 8 times per month.

Lets say the new member chooses 3 month fitness, I need to  a produce a data range for the duration of 3 month, that allows me to track the amount of times the member accessed the pool  within the date range. If the member accessed the pool 8 times, he will have to wait till he falls into the next date range, before he can access the pool again. I have attached a picture of what I am currently have. The result as seen in the picture was created by using my code originally posted.

In the image attached the actual member started his membership on 2011-11-28.

 User generated image
As you can see in the image, whatever date range we are currently in, the admit button is active. If the user would use 8 times swimming within this date range, the button would also become inactive. The member would have to wait then again till he falls into the next date range before access to the pool is granted. I hope this also explains why I want the days to be different by 1 day, because if the ending day of the previous date range would be the same as the starting day of the next date range, I would have 2 active admit buttons.

I hope this makes more sense? Please feel free to ask again.

Thank you.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of slickip

ASKER

I gave 250 points to john-formby and Ray_Paseur each. I didn't really manage to solve this little headache, but both were giving me great example codes and ideas which I can use to improve my own skills on.

Thank you again! Its always fun to be here.

Best Regards.
Avatar of slickip

ASKER

Thank you again!