PHP help

Discussion in 'PHP' started by deleted-account, Jun 20, 2009.

  1. #1
    I'll get right to the point

    Here is my code for generating the name of the month based on what the number of the month is.

    function displayMonth($num)
    {
    if ($num = 1) {$month = 'January';}
    else if ($num = 2) {$month = 'February';}
    else if ($num = 3) {$month = 'March';}
    else if ($num = 4) {$month = 'April';}
    else if ($num = 5) {$month = 'May';}
    else if ($num = 6) {$month = 'June';}
    else if ($num = 7) {$month = 'July';}
    else if ($num = 8) {$month = 'August';}
    else if ($num = 9) {$month = 'September';}
    else if ($num = 10) {$month = 'October';}
    else if ($num = 11) {$month = 'November';}
    else if ($num = 12) {$month = 'December';}
    else {echo '(!) Month Calculator error!';}
    
    echo $month;
    }
    Code (markup):
    Now for some reason it only displays it as January no matter what month I use!
    What is the error here? Or is there a better way of going about this?
     
    deleted-account, Jun 20, 2009 IP
  2. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #2
    Change

    if($num = 1)

    to

    if($num == 1)

    In fact you'd be better with the following;

    
    switch($num){
      case '1': 
        $month = 'January';
      break;
      case '2': 
        $month = 'February';
      break;
      case '3': 
        $month = 'March';
      break;
      case '4': 
        $month = 'April';
      break;
      case '5': 
        $month = 'May';
      break;
      case '6': 
        $month = 'June';
      break;
      case '7': 
        $month = 'July';
      break;
      case '8': 
        $month = 'August';
      break;
      case '9': 
        $month = 'September';
      break;
      case '10': 
        $month = 'October';
      break;
      case '11': 
        $month = 'November';
      break;
      case '12': 
        $month = 'December';
      break;
      default:
        echo "error";
      break;
    }
    
    echo $month;
    
    PHP:
     
    Weirfire, Jun 20, 2009 IP
    Ali Razaqpur likes this.
  3. deleted-account

    deleted-account Active Member

    Messages:
    655
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    85
    #3
    Thanks! It worked perfectly!
     
    deleted-account, Jun 20, 2009 IP
  4. Sudoku-Master

    Sudoku-Master Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    here a little "smarter" version of your function. In this version you can use setlocale() to switch the language for the returned value:

    setlocale(LC_TIME, "en_US"); # for english
    setlocale(LC_TIME, "de_DE"); # for german
    setlocale(LC_TIME, "fi_FI"); # for finnish
    ....

    
    function displayMonth($num)
    {
    $month = FALSE;
    $num=intval($num);
    if ($num>0 && $num<13)
    {
    $month = strftime('%B', mktime(10,0,0,$num));
    }
    return $month;
    }
    
    Code (markup):
     
    Sudoku-Master, Jun 20, 2009 IP
  5. Cloudberries

    Cloudberries Peon

    Messages:
    74
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Or, even quicker..

    echo ($num>0&&$num<=12)?date("F", mktime(0, 0, 0, $num+1, 0, 0)):"error";
    Code (markup):
     
    Cloudberries, Jun 20, 2009 IP
  6. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #6
    Beautiful solution! :)
     
    Weirfire, Jun 24, 2009 IP
    franklyn likes this.
  7. Sudoku-Master

    Sudoku-Master Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    but it is maybe a problem if $num= 3.56
     
    Sudoku-Master, Jun 24, 2009 IP
  8. franklyn

    franklyn Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    yeah i figured that php should have an internal function that does this for you.
     
    franklyn, Jun 24, 2009 IP
  9. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #9
    Don't be ridiculous!

    In any case;

    
    ceil($num) 
    
    PHP:
    would fix this problem for if you were really pernickety
     
    Weirfire, Jul 3, 2009 IP
  10. dweebsonduty

    dweebsonduty Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    Digital Goods:
    1
    #10
    I do this:

    $months = array(1 => 'January', 'February', 'March','April','May','June','July','August','September','October','November','December');
    
    $x=12;
    
    echo $months[$x];
    
    PHP:
     
    dweebsonduty, Jul 3, 2009 IP
  11. udskiii

    udskiii Peon

    Messages:
    143
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Bow Down Bow down Bow down

     
    udskiii, Jul 3, 2009 IP