2013-06-23 64 views
0

我想從mySQL數據庫(格式化YYYY-MM-DD)中獲取完整的日期,並讓PHP將2位數的月份數字轉換爲3個字母的表示形式,並將其放入選擇框供用戶編輯,當用戶選擇需要返回的月份作爲2位數月份號碼。下面的代碼很好用,除了由於某些原因08返回給用戶的是08而不是8月,9月的情況也是如此,但是其他所有月份都按照我的意願去做。你認爲這是PHP本身的一個小故障嗎?我很確定我沒有在這裏錯過任何東西。 在此先感謝!PHP無法正確轉換12個月中的2個... PHP Bug?

$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>"; 
+9

您正在尋找的搜索詞是「八進制」。 – DCoder

+3

簡短版本是:「不要在數字中使用前導零」。 – duskwuff

+0

爲什麼它會在一月到七月間運行,而不是八月和九月? –

回答

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 
+0

這樣做,謝謝一百萬! –

+0

沒問題,很高興我能幫到你。 – user2067005

+1

@codeguy你想標記爲已回答? –

2

如何:

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

如果你需要很長的名單,如果那麼很可能什麼是錯的。