www.webdeveloper.com
Results 1 to 5 of 5

Thread: Convert Month Number to Month Name in a Range

  1. #1
    Join Date
    Oct 2011
    Posts
    350

    Convert Month Number to Month Name in a Range

    Hi..

    I need help in getting the 3 Months Name from my table field FromMonth and ToMonth.
    Here is the scenario.

    First, I select FromMonth and ToMonth.
    Ex.
    FromMonth = 5
    ToMonth = 7
    So it means
    FromMonth is May
    ToMonth is July.

    Now I save the MonthNumber to my database:

    table- so_month
    FromMonth = 5
    ToMonth = 7

    Now I need to get the between Months Name from FromMonth to ToMonth which are (May, June, July).

    How does it possible?
    Thank you so much.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,331
    probably the simplest way would be to create an indexed array of month names, e.g.:
    PHP Code:
    $monthNames = array(
      
    => 'January',
      
    'February',
      
    'March',
      
    'April',
      
    'May',
      
    'June',
      
    'July',
      
    'August',
      
    'September',
      
    'October',
      
    'November',
      
    'December'
    ); 
    Then, for example, if you need the name of the 7th month:
    PHP Code:
    echo $monthNames[7]; 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Oct 2011
    Posts
    350
    Working days is SUM of workingdays of 3 months, I have code for getting the consecutive 3 months but now I got a problem in getting the SUM of working days.

    here is my code for that:


    PHP Code:
    echo "<tr><td>Working Days</td>";
    function 
    monthNames($from$to){
       
    $range=array();
       for(
    $i=$from$i<=$to$i++){
               
    $range[$i]=date('M'mktime(0,0,0,$i));
       }
        return 
    $range;
    }

    $sql "SELECT FromMonth, ToMonth FROM so_month";
    $res mysql_query($sql,$con);

    $row mysql_fetch_assoc($res);
    $FromMonth $row['FromMonth'];
    $ToMonth $row['ToMonth'];
     

    foreach( 
    monthNames($FromMonth$ToMonth) as $month){ echo $month,'<br>';  
    $sql "SELECT MonthName, SUM(WorkingDays) AS WorkingDays FROM working_days WHERE Month_Name = '$month' ORDER BY MonthName";
    $res mysql_query($sql$con);

    $row mysql_fetch_assoc($res);

    $WorkingDays $row['WorkingDays'];

    echo 
    "<td>$WorkingDays</td>";


    but I got this error:

    Apr

    Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in D:\Program Files\xampp\htdocs\MES PROJECT\HalfShellParam.php on line 147
    May

    Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in D:\Program Files\xampp\htdocs\MES PROJECT\HalfShellParam.php on line 147
    Jun

    Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in D:\Program Files\xampp\htdocs\MES PROJECT\HalfShellParam.php on line 147

    Thank you

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,331
    One of your queries is failing to process, so add some debug code to find out why:
    PHP Code:
    $res mysql_query($sql);
    if(
    $res == false) {
       throw new 
    Exception("Query failed: ".mysql_error()."\n$sql");

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Oct 2011
    Posts
    350
    Hi..

    I resolved it now using thid code:

    PHP Code:
    //-----Computation of Working Days---//
    $sql "SELECT FromMonth, ToMonth FROM so_month";
    $res mysql_query($sql,$con);

    $row mysql_fetch_assoc($res);
    $FromMonth $row['FromMonth'];
    $ToMonth $row['ToMonth'];

    function 
    monthNames($from$to){
       
    $range=array();
       for(
    $i=$from$i<=$to$i++){
               
    $range[$i]=date('M'mktime(0,0,0,$i));
       }
        return 
    $range;
    }
     
    $month_ implode("' ', ",monthNames($FromMonth,$ToMonth));

    foreach( 
    monthNames($FromMonth$ToMonth) as $month){ 
    $sql "SELECT MonthName, SUM(WorkingDays) AS WorkingDays FROM working_days WHERE MonthName IN ('$month') GROUP BY MonthName";
    $res mysql_query($sql$con);
    $SumWorkingDays 0
    while(
    $row mysql_fetch_array($res)){

     
    $WorkingDays+=(int)$row['WorkingDays'];
    $SumWorkingDays += $WorkingDays;

    }

    Thank you

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles