2013-10-25 80 views
1

我在php中使用strtotime函數有問題,我試圖將mm-dd-yyyy轉換爲yyyy-mm-dd。日期在文本框中輸入,並在提交時將日期保存到數據庫。問題是它返回一個錯誤的日期(1970-01-01)每一次,這意味着我的代碼沒有采取我用它來存儲日期變量,我的代碼是:strtotime返回假日期

//dateconvert 
$submitdate = date($_POST['date']); 
$date = date("Y-m-d", strtotime($submitdate)); 

//storeindb 
$query ="INSERT INTO ticket SET date = '$date'"; 
$result = mysql_query($query); 

我是新手,請幫忙。

+1

'MM-DD-yyyy'不是格式'strtotime'可以翻譯,應該是'MM/DD/yyyy'(解釋爲美國日期)或'DD -mm-yyyy'(解釋爲歐洲或至少意大利語的日期) –

+0

$ _POST ['date']的值是什麼? – ModulusJoe

+0

@MatteoTassinari我使用了mm/dd/yyyy,但它仍然返回相同的值 –

回答

2

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.

http://php.net/manual/en/function.strtotime.php

您需要使用MM/DD/YYYY格式斜槓分隔。

1

使用代碼:

$submitdate = $_POST['date']; 
$date = date("Y-m-d", strtotime($submitdate)); 

希望這hepls。

1

日期格式$submitdate不正確因爲格式yyyy/mm/dd應該是yyyy-mm-dd,所以您需要替換/字符。

嘗試這種情況:

$submitdate = str_replace("/","-",$_POST['date']); 
echo date('Y-m-d', strtotime($submitdate)); 
+0

這我以前使用...沒有工作.. 。返回的值與之前的 –

+0

相同,那麼我認爲你的日期必須是'dd-mm-yyyy'格式。如果是這樣,請將其更改爲'yyyy-mm-dd'格式。 –