Link to home
Start Free TrialLog in
Avatar of Umesh
UmeshFlag for India

asked on

Split month into weeks

Hi,

I'm looking for a PHP function which should split a given month into weeks and when week selected should give date range i.e start date of the week and end date of the week.

Let me clear my requirement - Suppose I selects a month from the drop down it should then display the week drop down as week1,week2 etc and when I select week1 then it should give the date range.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America 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
SOLUTION
Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland 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 Umesh

ASKER

Thanks Ray,bportlock.

I think you guyz are pointing me in right direction but pls note I'm a Hybrid DBA so I might not be getting what you are trying to say... :-)    Actually was preparing a DB usage report and wanted to provide a  month option at the start and once the month is selected based on that want to give a option to view the data for the first week/seconnd week etc.... Can this be done? if so please let me know how. :-)

Sure it can be done.  But your own definitions of "week" and "month" are important.

Do you want the first 7 days to be the first week?

Do you want the last 7 days to be the last week?

The number of days in months are usually not divisible by 7.  What should become of the other days?

Those are kind of "constitutional" questions for this exercise.
The first thing is picking the starting day. Once you have that then a lot of the other stuff follows on. For instance

$startDay = ..... some relevant date in (say) yyyy-mm-dd format

$week1 = $startDay;
$week2 = date("Y-m-d", strtotime("$startDay +7 DAYS") );
$week3 = date("Y-m-d", strtotime("$startDay +14 DAYS") );
$week4 = date("Y-m-d", strtotime("$startDay +21 DAYS") );
$week5 = date("Y-m-d", strtotime("$startDay +28 DAYS") );

Now it is obvious that if your start day is the first day of the month then "week 5" will be anything from 0 (in February) to 3 days (29, 30 and 31) so you might need to "average" that week up to make a comparison with ealier weeks valid.

If you decide to start on the first MONDAY then the Monday could be as late as the 7th of the month.

It seems to me that the simplest option is to run from the first of the month for 4 full weeks and where there is a fifth week to scale up the data and mark that week as "not a full week - estimated data" so that if you have (say) 3 days of data then you scale up by 7/3.

If you try and run with the first Monday then you might get calendar slippage, overlaps and all sorts of confusion.
Avatar of Umesh

ASKER

>Do you want the first 7 days to be the first week?
>Do you want the last 7 days to be the last week?

Not exactly,  Looking similar to below function

http://www.php.net/manual/en/function.date.php#84145
Try using this as a starting point.  You can install it and experiment with it to see what you get for the counterintuitive examples like the 5th week of February.

Best regards, ~Ray
<?php // RAY_temp_ushastry.php
error_reporting(E_ALL);
date_default_timezone_set('America/Chicago');
echo "<pre>";


function monthly_weeks()
{
    // ASSUME THIS YEAR
    $y = date('Y');

    // TEST FOR REASONABLE VALUES
    if (!$x = strtotime($y . '-' . $_GET["month"] . '-01')) return FALSE;

    // DETERMINE THE FIRST DAY OF THE MONTH
    $f = date('Y-m-d', $x);

    // IF THE LAST WEEK IS REQUESTED
    if ($_GET["week"] == 'LL')
    {
        $l = date('Y-m-t', strtotime($f));
        $w = date('Y-m-d', strtotime($l . ' - 6 DAYS'));
    }
    // ADD THE WEEKS AND GET THE LAST DAY
    else
    {
        $w = date('Y-m-d', strtotime($f . ' +' . $_GET["week"] . ' WEEKS'));
        $l = date('Y-m-d', strtotime($w . ' + 6 DAYS'));
    }
    return array ( 'A' => $w, 'Z' => $l );
}



// RUN THE FUNCTION
if (!empty($_GET))
{
    var_dump(monthly_weeks());
}


// END OF PHP - PUT UP A FORM
?>
<form method="GET">
<select name="month">
<option value="00" selected="selected">CHOOSE MONTH</option>
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
</select>

<select name="week">
<option value="00" selected="selected">CHOOSE WEEK</option>
<option value="00">First</option>
<option value="01">Second</option>
<option value="02">Third</option>
<option value="03">Fourth</option>
<option value="04">Fifth</option>
<!-- SPECIAL CASE -->
<option value="LL">Last</option>
</select>

<input type="submit" value="COMPUTE DATES" />
</form>

Open in new window

Looking at this is confusing to me:
http://www.php.net/manual/en/function.date.php#84145

Try running this little script.  As you can see the first week of January is split into week 53 of 2009 and week 1 of 2010.  So your desired definition of what is the "first" week needs to take this kind of thing into account.

HTH, ~Ray
<?php // RAY_temp_ushastry.php
error_reporting(E_ALL);
date_default_timezone_set('America/Chicago');
echo "<pre>";

$alpha = date('Y-m-d', strtotime('January 1, 2010'));
$omega = date('Y-m-d', strtotime('April 15, 2010'));

while ($alpha < $omega)
{
   $ts = strtotime($alpha);
   echo PHP_EOL;
   echo date('D d M Y', $ts);
   echo ' IS WEEK NUMBER ';
   echo date('W', $ts);
   $alpha = date('Y-m-d', strtotime($alpha . ' + 1 DAY'));
}

Open in new window

Avatar of Umesh

ASKER

Thanks for your help,  at this point of time I feel instead of  this Month->Week'N' option better to just  allow to enter/select dates in range(from and to)...

You guyz mind if I split the point between both of you?  pls let me know..
I think letting the user enter the dates is a good idea - much more flexible.  You might even have a jQuery date picker, if you're into that sort of thing.

Cheers, ~Ray
Avatar of Umesh

ASKER

Thanks...
"...jQuery date picker"

Or a MooTools one - there are some nice ones out there....

@ushastry - given the that Gregorian calendar is such a mess just using date ranges or even whole months is a better idea. Failing that use the 1st of the month as the start of your week and estimate the last week, but to try and generalise the weeks is going to be so messy that I cannot see it being either informative or worth the effort.

As to the points - it's up to you. I won't argue with however you award them.
I see you sorted it out while I was typing...

;-)