SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,820
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)
    Use the date() when outputing.
    For example.... this foo() would give you an array of the month names.

    PHP Code:
    function monthNames($from$to){
       
    $range=array();
       for(
    $i=$from$i<=$to$i++){
               
    $range[$i]=date('M'mktime(0,0,0,$i));
       }
        return 
    $range;
    }
     echo  
    implode(", ",monthNames(5,7)); 
    hope that helps

  3. #3
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank's it works..

    Now i have an issue..

    Is their any way so I can select the first consecutive months in my table sales_order
    fields - ETD type - Date.

    Thank you

  4. #4
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried this:

    PHP Code:
    function monthNames($from$to){
       
    $range=array();
       for(
    $i=$from$i<=$to$i++){
               
    $range[$i]=date('M'mktime(0,0,0,$i));
       }
        return 
    $range;
    }
     echo  
    implode(", ",monthNames(5,7));
     
     
    $sql "SELECT FromMonth, ToMonth FROM so_month";
    $res mysql_query($sql,$con);

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

    echo 
    $FromMonth;
    echo 
    $ToMonth;
     
     echo 
    monthNames($FromMonth$ToMonth); 
    the output is:

    May, Jun, Jul57Array

    it did not read the $FromMonth, $ToMonth

    why?

    Thank you

  5. #5
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,820
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)
    this foo() would give you an array of the month names.
    so what is happening is :monthNames(5, 7)=Array( 5=>'may', 6=>'june', 7=>'july')
    Arrays can't be echoed.

    If you wanted to echo them all in one statement, use implode(); ( or change the function to store the month in a string and not an array, tho I think you would find this would trully limit the functions versatility.

    You could also do this:

    foreach( monthNames($FromMonth, $ToMonth) as $month){ echo $month,'<br>'; }

  6. #6
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok thank you..

    Now how can I put the 3 months in table <td>

    for example:
    May
    June
    Jul

    Thank you

  7. #7
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,820
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)
    same way...
    what script are you using to build your table? You will need to take into account how HTML tables work. but just for the sake of showing you how to output:

    foreach( monthNames($FromMonth, $ToMonth) as $month){ echo '<td>',$month,'</td>'; }

  8. #8
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,340
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by newphpcoder View Post
    Is their any way so I can select the first consecutive months in my table sales_order
    fields - ETD type - Date.
    somebody else had exactly the same question!

    http://www.sitepoint.com/forums/show...=1#post5108991
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  9. #9
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you


Bookmarks

Posting Permissions

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