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.



slickipAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

xtermCommented:
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.

0
john-formbyCommented:
Hi slickip,

I am guessing the first date you showed in the example is incorrect as it does not follow the logic for the rest of the displayed data.

Please check out the code below and let me know if this is what you require.

Kind Regards,

John
<?php
$dstart = '2011-12-30';
$swmlength = 4;
for($i=1; $i<=$swmlength;$i++) {
    echo date('Y-m-d',mktime(0,0,0,date('m',strtotime($dstart))+$i,1,date('Y',strtotime($dstart)))).' ---- '.
    date('Y-m-t',mktime(0,0,0,date('m',strtotime($dstart))+$i,1,date('Y',strtotime($dstart)))).'<br />';
}
?>

Open in new window

0
slickipAuthor Commented:
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!
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

slickipAuthor Commented:
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.
0
john-formbyCommented:
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
0
slickipAuthor Commented:
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.

 Current swim tracking
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.
0
Ray PaseurCommented:
See http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

The meaning of "month" is inherently ambiguous when it is measured in days.  In my experience, about the best you can do is use the built-in PHP functions like this:

$today = date('c')
$future = date('c', strtotime($today . ' + 3 MONTHS'));

You might find that you can use the nextmonth() function successfully.  See the code snippet.  You can see it in action on my server here:
http://www.laprbass.com/RAY_ambiguous_month.php

HTH, ~Ray
<?php // RAY_ambiguous_month.php
error_reporting(E_ALL);
echo "<pre>";

// REQUIRED FOR PHP 5.1+
date_default_timezone_set('America/New_York');

// TEST DATES INCLUDE SOME THAT MIGHT CAUSE AMBIGUITY WHEN YOU SAY "next month"
$my_dates
= array
( 'January 28, 2011'
, 'January 29, 2011'
, 'January 30, 2011'
, 'January 31, 2011'
, 'February 1, 2011'
, 'February 2, 2011'
, 'February 28, 2011'
, 'March 30, 2011'
, 'March 31, 2011'
)
;

// TEST EACH OF THE DATES
foreach ($my_dates as $my_date)
{
    list
    ( $safe_date
    , $requested_day
    , $actual_day
    ) = nextMonth($my_date, 'r');
    echo PHP_EOL . "MY_DATE $my_date";
    echo PHP_EOL . "NEXTMONTH $safe_date";
    echo PHP_EOL . "REQUESTED DAY $requested_day";
    echo PHP_EOL . "ACTUAL DAY $actual_day";
    echo PHP_EOL;
}


function nextMonth($date, $format='c')
{
    $timestamp  = strtotime($date);
    $start_Y    = date('Y', $timestamp);
    $start_m    = date('m', $timestamp);
    $start_d    = date('d', $timestamp);

    // MAKE A TIMESTAMP FOR THE FIRST, LAST AND REQUESTED DAY OF NEXT MONTH
    $timestamp_first = mktime(0,0,0, $start_m+1,  1, $start_Y);
    $timestamp_last  = mktime(0,0,0, $start_m+1, date('t', $timestamp_first), $start_Y);
    $timestamp_try   = mktime(0,0,0, $start_m+1, $start_d, $start_Y);

    // USE THE LESSER OF THE REQUESTED DAY AND THE LAST OF THE MONTH
    if ($timestamp_try > $timestamp_last) $timestamp_try = $timestamp_last;
    $good_date = date($format, $timestamp_try);

    return array
    ( $good_date
    , $start_d
    , date('d', $timestamp_try)
    )
    ;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
slickipAuthor Commented:
Hi again.

Well. It seems I wasn't able to come up with anything at this time. I did however change my code to this:

$dstart = '2011-11-30';
$swmlength = 5;

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

$startdate = date('Y-m-d',strtotime(+$counter." month ",strtotime($dstart)));
$startdate = date('Y-m-d',strtotime("-1 month",strtotime($startdate)));

$enddate = date('Y-m-d',strtotime(+$counter." month", strtotime($dstart)));

echo $startdate.' ---- '.$enddate.'<br />';

Open in new window


This will produce the following:
2011-11-30 ---- 2011-12-30
2011-12-30 ---- 2012-01-30
2012-02-01 ---- 2012-03-01
2012-03-01 ---- 2012-03-30
2012-03-30 ---- 2012-04-30

Open in new window


I will stay with this for now. Maybe, in the near future, when I get more proficient with PHP, I will manage to create a solution.

Thanks to all.

Regards.
0
slickipAuthor Commented:
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.
0
slickipAuthor Commented:
Thank you again!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.