我有這個日期輸入:如何將php日期格式轉換爲3個參數
$_POST['date']; output is : 2013/10/10
。
現在我需要在此日期格式轉換成3個參數:
$year = '2013';
$month = '10';
$date = '10';
如何創建呢?
我有這個日期輸入:如何將php日期格式轉換爲3個參數
$_POST['date']; output is : 2013/10/10
。
現在我需要在此日期格式轉換成3個參數:
$year = '2013';
$month = '10';
$date = '10';
如何創建呢?
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');
對我來說太快了! –
可以使用explode()功能
$date = explode("/", $_POST['date']);
echo $date[0]; //Year
echo $date[1]; //month
echo $date[2]; //Day
的爆炸()函數的字符串分割字符串,使一個數組
對於PHP 5> = 5.3.0 http://www.php.net/manual/en/datetime.createfromformat.php
$d = DateTime::createFromFormat("Y/m/d", $_POST['date']);
$year = $d->format("Y");
$month = $d->format("m");
$day = $d->format("d");
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
試試這個http://stackoverflow.com/questions/15920768/how-to-re-format-datetime-string-in-php/15920983#15920983 –