2010-02-09 82 views
4

我試圖將2010-02轉換爲2010年2月。但是,我不斷收到1969年12月php - mktime或strtotime?

我試過使用mktime,strtotime和這兩者的某種組合,但仍然沒有能夠做到這一點......

這是我試過最近...

$path_title = date('F, Y', mktime(0,0,0,2,0,2010)); 

回答

8

這將是一個辦法做到這一點:

$dateString = '2010-02'; 
list($year, $month) = explode('-', $dateString); 
$timeStamp = mktime(0, 0, 0, $month, 1, $year); 
echo date('F, Y', $timestamp); 

另一種方法是:

$dateString = '2010-02'; 
$timestamp = strtotime($dateString . '-01'); 
echo date('F, Y', $timestamp); 

strtotime不能處理不明確的日期,如 「2010-02」,但如果你做它應該工作的完整日期。

否則,你可能想看看像DateTime::createFromFormat

+0

感謝您的支持。而對於鏈接,因爲我認爲這對未來會有幫助! – n00b0101 2010-02-09 02:49:33

0

試試這個:

$str = '2010-02'; 
echo date('F, Y',mktime(0,0,0,substr($str,-2),1,substr($str,0,4))); 

,你必須確保你使用VAL ID值爲mktime()。在你編輯問題的例子中,你有0作爲一天,實際上是第一天減去一天,這會讓你進入前一個月。