2013-10-25 212 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'格式。 –