strtotime返回虚假日期

时间:2013-10-25 10:17:23

标签: php date strtotime

我在php中有strtotime函数的问题,我正在尝试将mm-dd-yyyy转换为yyyy-mm-dd。日期在文本框中输入,提交时应将日期保存到数据库中。问题是它每次都返回一个错误的日期(1970-01-01),这意味着我的代码没有采用我用来存储日期的变量,我的代码是:

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

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

我是新手,请帮忙。

3 个答案:

答案 0 :(得分: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 :(得分:1)

使用以下代码:

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

希望这有帮助。

答案 2 :(得分:1)

<{1>}中的日期格式不正确因为格式为yyyy / mm / dd应为yyyy-mm-dd所以您需要替换$submitdate个字符。

试试这个:

/
相关问题