2014-01-18 22 views
0

我有這個日期輸入:如何將php日期格式轉換爲3個參數

$_POST['date']; output is : 2013/10/10

現在我需要在此日期格式轉換成3個參數:

$year = '2013'; 

$month = '10'; 

$date = '10'; 

如何創建呢?

+0

試試這個http://stackoverflow.com/questions/15920768/how-to-re-format-datetime-string-in-php/15920983#15920983 –

回答

6

您可以使用explode()list結構,像這樣:

list($year, $month, $date) = explode('/', $_POST['date']); 

但是,這不是一個好主意。我建議使用DateTime類的日期和時間工作時:

$str = "2013/10/10"; 
$dateObj = DateTime::createFromFormat('Y/m/d', $str); 

$month = $dateObj->format('m'); 
$year = $dateObj->format('Y'); 
$date = $dateObj->format('d'); 
+1

對我來說太快了! –

0

可以使用explode()功能

$date = explode("/", $_POST['date']); 
echo $date[0]; //Year 
echo $date[1]; //month 
echo $date[2]; //Day 

的爆炸()函數的字符串分割字符串,使一個數組

1

OK試試這個它的工作對我來說:

CODING :

<?PHP 
     $your_date = $_POST['date']; 
     echo "year =".date("Y", strtotime($your_date)); 
     echo "month =".date("m", strtotime($your_date))"; 
     echo "day =".date("d", strtotime($your_date)); 
    ?> 

Dron

相關問題