Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Sep 2006
    Location
    India Mumbai
    Posts
    248
    Thanks
    13
    Thanked 1 Time in 1 Post

    months and year between to range

    hi all,
    i have to generate a report between any two months and year [for financial results maybe].
    for that i have given drop downs to select monthfrom , yearfrom , monthto and yearto

    how will i find out months with year between any two dates.

    Thanks in advance

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    It depends on how you're storing the information you want to search for(database?), and what format their dates are in. The easiest way should be to store the information in a database like MySQL that has date functions to allow you to select date ranges quite easily.

  • #3
    Regular Coder
    Join Date
    Sep 2006
    Location
    India Mumbai
    Posts
    248
    Thanks
    13
    Thanked 1 Time in 1 Post
    hi Inigoesdr,


    Quote Originally Posted by Inigoesdr View Post
    It depends on how you're storing the information you want to search for(database?), and what format their dates are in. The easiest way should be to store the information in a database like MySQL that has date functions to allow you to select date ranges quite easily.
    In the report the user has the select option as below:
    <FORM METHOD=POST ACTION="" name="mainform" onsubmit="return validate();">

    <tr>
    <td class="header">From Month</td>
    <td class="header">From Year</td>
    <td class="header">To Month</td>
    <td class="header">To Year</td>
    <td class="header">Option</td>
    </tr>
    <tr>
    <td>
    <SELECT NAME="monthfrom">
    <option value="">-- Select --</option>
    <option value="01" <? if ($monthfrom=='01') { print 'selected'; } ?>>Jan</option>
    <option value="02" <? if ($monthfrom=='02') { print 'selected'; } ?>>Feb</option>
    <option value="03" <? if ($monthfrom=='03') { print 'selected'; } ?>>Mar</option>
    <option value="04" <? if ($monthfrom=='04') { print 'selected'; } ?>>Apr</option>
    <option value="05" <? if ($monthfrom=='05') { print 'selected'; } ?>>May</option>
    <option value="06" <? if ($monthfrom=='06') { print 'selected'; } ?>>Jun</option>
    <option value="07" <? if ($monthfrom=='07') { print 'selected'; } ?>>Jul</option>
    <option value="08" <? if ($monthfrom=='08') { print 'selected'; } ?>>Aug</option>
    <option value="09" <? if ($monthfrom=='08') { print 'selected'; } ?>>Sep</option>
    <option value="10" <? if ($monthfrom=='10') { print 'selected'; } ?>>Oct</option>
    <option value="11" <? if ($monthfrom=='11') { print 'selected'; } ?>>Nov</option>
    <option value="12" <? if ($monthfrom=='12') { print 'selected'; } ?>>Dec</option>
    </SELECT>
    </td>
    <td>
    <SELECT NAME="yearfrom">
    <option value="">-- Select --</option>
    <? for ($i=2001 ; $i<=(strftime("%Y")+5); $i++) { ?>
    <option value="<?=$i?>" <? if ($yearfrom==$i) { print 'selected'; } ?>><?=$i?></option>
    <? } ?>
    </SELECT>
    </td>
    <td>
    <SELECT NAME="monthto">
    <option value="">-- Select --</option>
    <option value="01" <? if ($monthto=='01') { print 'selected'; } ?>>Jan</option>
    <option value="02" <? if ($monthto=='02') { print 'selected'; } ?>>Feb</option>
    <option value="03" <? if ($monthto=='03') { print 'selected'; } ?>>Mar</option>
    <option value="04" <? if ($monthto=='04') { print 'selected'; } ?>>Apr</option>
    <option value="05" <? if ($monthto=='05') { print 'selected'; } ?>>May</option>
    <option value="06" <? if ($monthto=='06') { print 'selected'; } ?>>Jun</option>
    <option value="07" <? if ($monthto=='07') { print 'selected'; } ?>>Jul</option>
    <option value="08" <? if ($monthto=='08') { print 'selected'; } ?>>Aug</option>
    <option value="09" <? if ($monthto=='09') { print 'selected'; } ?>>Sep</option>
    <option value="10" <? if ($monthto=='10') { print 'selected'; } ?>>Oct</option>
    <option value="11" <? if ($monthto=='11') { print 'selected'; } ?>>Nov</option>
    <option value="12" <? if ($monthto=='12') { print 'selected'; } ?>>Dec</option>
    </SELECT>
    </td>

    <td>
    <SELECT NAME="yearto">
    <option value="">-- Select --</option>
    <? for ($i=2001 ; $i<=(strftime("%Y")+5); $i++) { ?>
    <option value="<?=$i?>" <? if ($yearto==$i) { print 'selected'; } ?>><?=$i?></option>
    <? } ?>
    </SELECT>
    </td>
    <td><INPUT TYPE="submit" name="submit" value="Submit"></td>
    </tr>
    </table>
    for eg if the user selects April 2007 - March 2008
    So here i want a function which will return me april 2007 , May 2007 .... up to March 2008....
    Thanks

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    You still haven't said how you're storing the data or what format you need the dates in to compare them to the data.

  • #5
    Regular Coder
    Join Date
    Sep 2006
    Location
    India Mumbai
    Posts
    248
    Thanks
    13
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Inigoesdr View Post
    You still haven't said how you're storing the data or what format you need the dates in to compare them to the data.
    hi Inigoesdr,
    i am storing the data in tables in mysql
    i found a function which returns me months between two dates. but if you check the output its not functioning rightly. can you please help me in rectifying it....

    function get_months($date1, $date2) {
    $time1 = strtotime($date1);
    $time2 = strtotime($date2);
    $my = date('mY', $time2);
    $months = array(date('F', $time1));

    while($time1 < $time2) {
    $time1 = strtotime(date('Y-m-d', $time1).' +1 month');
    if(date('mY', $time1) != $my && ($time1 < $time2))
    $months[] = date('F', $time1);
    }

    $months[] = date('F', $time2);
    print_r($months);
    return $months;
    }

    check this out:

    if($submit){
    $mth=$monthfrom;
    $stdate=$yearfrom."-".$monthfrom."-01";
    //$eddate=$yearto."-".$monthto."-01";
    $eddate=date('Y-m-d',mktime(0, 0, 0, (int)$monthto+1, 0, $yearto));
    $monthsArr=(count(get_months($stdate, $eddate)));
    $months=(count(get_months($stdate, $eddate)));
    print_r($monthsArr);
    }

    if i print the array in case of May 2006 - May 2006 it returns me Array ( [0] => May [1] => May )
    it should be only May. this problem is only when i select from month and to month same

  • #6
    Regular Coder
    Join Date
    Sep 2006
    Location
    India Mumbai
    Posts
    248
    Thanks
    13
    Thanked 1 Time in 1 Post
    Finally i got a perfect code to calculate the months between two range. just i added the bold lines to rectify the problem of repetation when the start month and end month is same.....

    function get_months($date1, $date2) {
    $time1 = strtotime($date1);
    $time2 = strtotime($date2);
    $my = date('mY', $time2);
    $my1 = date('mY', $time1);
    $my2 = date('mY', $time2);
    $months = array(date('F', $time1));


    if ($my1== $my2) {
    return $months;
    }

    while($time1 < $time2) {
    $time1 = strtotime(date('Y-m-d', $time1).' +1 month');
    if(date('mY', $time1) != $my && ($time1 < $time2))
    $months[] = date('F', $time1);
    }

    $months[] = date('F', $time2);
    // print_r($months);
    return $months;
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •