2012-08-06 23 views
0

我想將該數字轉換爲實際的月份。我只需要一個月。如何將日期從8轉換爲AUG

我現在想這樣

$str = 8; $anInt = intval($str); $test = date('M',$anInt); echo $test;

結果是JAN,我應該得到 「AUG」 作爲結果。我不知道爲什麼。

對我有什麼想法或建議嗎?

+0

究竟做'date'接受作爲第二個參數?請在文檔中檢查它 – zerkms 2012-08-06 11:33:51

+0

我的回答編輯向您展示您的確切示例。 – Fluffeh 2012-08-06 11:39:36

回答

2

date()函數假設時間戳。

你應該試試這個:

$test = date('M',mktime(0,0,0,$anInt,0,0); 

從文檔爲datemktime

// Prints: Your date's month is : August 
echo "Your date's month is : " . date("F", mktime(0, 0, 0, $anInt, 0, 0)); 
0

使用mktime

echo date('M', mktime(0, 0, 0, $str)); 
0

給你:

echo date('M',strtotime('08/01/2012')); 

或者,如果你想全部大寫:

echo strtoupper(date('M',strtotime('08/01/2012'))); 

可能有其他的方法來這一點,但是這是第一個浮現在腦海中

+1

注意:8月1日是8月8日?我建議使用Y-m-d格式。 – Waygood 2012-08-06 11:45:24

+0

夠公平的,我有我的ini設置爲'm/d/Y'格式,應該指出 – 2012-08-06 12:41:39

0

重複的問題。 Read here.

閱讀關於日期函數here

使用date()您應該使用時間戳。 迄今爲止值轉換爲時間戳,使用strtotime

$date = '2012-8-8'; 
$timestamp = strtotime($date); 
echo date('M',$timestamp); 

對於您的問題:

$monthnumber = 4; 
$date = '2012-'.$monthnumber.'-8'; 
$timestamp = strtotime($date); 
echo date('M',$timestamp);