简体   繁体   English

strtotime返回虚假日期

[英]strtotime returning false date

I have a problem with strtotime function in php, i'm trying to convert mm-dd-yyyy to yyyy-mm-dd. 我在php中有strtotime函数的问题,我正在尝试将mm-dd-yyyy转换为yyyy-mm-dd。 The date is entered on the text box and on submit it should save the date to the database. 日期在文本框中输入,提交时应将日期保存到数据库中。 the problem is it returns a wrong date(1970-01-01) each time,which means my code isn't taking the variable i use to store the date, My code is: 问题是它每次都返回一个错误的日期(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);

I'm a newbie,please help. 我是新手,请帮忙。

Dates in the m/d/y or dmy 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 dmy format is assumed.

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

You need to use the forward slash separator for mm/dd/yyyy formats. 您需要使用mm / dd / yyyy格式的正斜杠分隔符。

Use the code as: 使用代码:

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

Hope this hepls. 希望这个肝脏。

date format in $submitdate is incorrect Because the format yyyy/mm/dd should be yyyy-mm-dd so you will need to replace your / characters. $submitdate日期格式不正确因为格式为yyyy / mm / dd应为yyyy-mm-dd所以您需要替换/字符。

Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM