0

我正在尝试从 mySQL 数据库中获取完整的日期(格式为 YYYY-MM-DD),并让 PHP 将 2 位月份数字转换为 3 位字母表示,并将其放入选择框中供用户编辑,当用户选择一个月份,它需要作为 2 位数的月份数返回。下面的代码效果很好,除了由于某种原因 08 以 08 而不是 AUG 的形式返回给用户,9 月也会发生同样的情况,但是所有其他月份都按照我的意愿行事。您认为这是 PHP 本身的故障吗?我很确定我在这里没有错过任何东西。提前致谢!

    $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>";
4

2 回答 2

2

怎么样:

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

如果您需要很长的 if 列表,那么可能有问题。

于 2013-06-23T07:47:37.403 回答
1

尝试这个:

$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
于 2013-06-23T07:03:20.147 回答