2011-06-22 57 views

回答

7

隨着strtok

$year = strtok($date, '-'); 

如果你想在一年整,你也可以使用intval

$year = intval($date); 
18

可能更昂貴,但可能更靈活,使用的strtotime()轉換爲一個時間戳和日期()中提取所需日期的一部分。

$year = date('Y', strtotime($in_date)); 
+0

'$ year = date('Y',strtotime(「2012」));'現在給我使用php 5.2。 –

1

繼承人一個簡單而確切的方法。

//suppose 
$dateProvided="1979-06-13"; 
//get first 4 characters only 
$yearOnly=substr($dateProvided,0,4); 
echo $yearOnly; 
//1979 

還有一件事要知道,在某些情況下,像,例如當日期是像2010-00-00預期的日期功能不起作用,它會返回2009年而不是2010年。 繼承人一個例子

//suppose 
$dateProvided="2010-00-00"; 
$yearOnly = date('Y', strtotime($dateProvided)); 
//we expect year to be 2010 but the value of year would be 2009 
echo $yearOnly; 
//2009 
相關問題