code guy
code guy

Reputation: 11

PHP not converting 2 out of 12 months correctly...PHP Bug?

I'm trying to take a complete date from mySQL database (formatted YYYY-MM-DD) and have PHP convert the 2 digit month number to a 3 letter representation and put it in a select box for a user to edit, when the user selects a month it needs to be returned as a 2 digit month number. This code below works great except that for some reason 08 comes back to the user as 08 and not AUG, the same occurs for September however all other months do what I want them to. Do you think this is a glitch with PHP itself? I'm pretty sure I didn't miss anything here. Thanks in advance!

    $endmonth = (substr($record['twpEndDate'], -5, 2));
    $endmonthnumber = (substr($record['twpEndDate'], -5, 2));
    if ($endmonth==00) {$endmonth=''; $endmonthnumber='';}
    else
    if ($endmonth==01) {$endmonth='JAN'; $endmonthnumber='01';}
    else
    if ($endmonth==02) {$endmonth='FEB'; $endmonthnumber='02';}
    else
    if ($endmonth==03) {$endmonth='MAR'; $endmonthnumber='03';}
    else
    if ($endmonth==04) {$endmonth='APR'; $endmonthnumber='04';}
    else
    if ($endmonth==05) {$endmonth='MAY'; $endmonthnumber='05';}
    else
    if ($endmonth==06) {$endmonth='JUN'; $endmonthnumber='06';}
    else
    if ($endmonth==07) {$endmonth='JUL'; $endmonthnumber='07';}
    else
    if ($endmonth==08) {$endmonth='AUG'; $endmonthnumber='08';}
    else
    if ($endmonth==09) {$endmonth='SEP'; $endmonthnumber='09';}
    else
    if ($endmonth==10) {$endmonth='OCT'; $endmonthnumber='10';}
    else
    if ($endmonth==11) {$endmonth='NOV'; $endmonthnumber='11';}
    else
    if ($endmonth==12) {$endmonth='DEC'; $endmonthnumber='12';}
echo "

<select name='twpEndMonth'>
<option value=" .$endmonthnumber. " style='display:none; selected'>" .$endmonth. "</option>
<option value='01'>JAN</option>
<option value='02'>FEB</option>
<option value='03'>MAR</option>
<option value='04'>APR</option>
<option value='05'>MAY</option>
<option value='06'>JUN</option>
<option value='07'>JUL</option>
<option value='08'>AUG</option>
<option value='09'>SEP</option>
<option value='10'>OCT</option>
<option value='11'>NOV</option>
<option value='12'>DEC</option>
</select>";

Upvotes: 0

Views: 83

Answers (2)

Jakub Kania
Jakub Kania

Reputation: 16477

How about:

$endmonth = strtoupper(date('M',strtotime($record['twpEndDate'])));
$endmonthnumber = (substr($record['twpEndDate'], -5, 2));

If you need a long if list then probably something is wrong.

Upvotes: 2

user2067005
user2067005

Reputation: 839

Try this:

$endmonth = explode("-", $record['twpEndDate']);
$endmonthnumber = '';//your code was getting a pointless number

if($endmonth[1] == '01'){$endmonth='JAN'; $endmonthnumber='01';}
elseif($endmonth[1] == '02'){$endmonth='FEB'; $endmonthnumber='02';}

//and so on

Upvotes: 1

Related Questions