如何在PHP中将时间格式从M-d-Y更改为Y-M-d?

时间:2014-12-02 11:58:11

标签: php date mysqli

我从日历中获取M-d-Y格式的输入,但是当它存储在数据库中时我想将其转换为Y-m-d。我可以使用什么方法?

4 个答案:

答案 0 :(得分:1)

试试这个:

$date="05-25-201";
print(date("y-m-d",strtotime($date)));

答案 1 :(得分:0)

你可以试试这个。 strtotime是你的朋友。

echo date("Y-m-d", strtotime('Dec-02-2014'));

或mysql DATE_FORMAT函数,但mysql以yyyy-mm-dd格式存储日期。

答案 2 :(得分:0)

使用date()strtotime()之类的:

 $var = date('Y-m-d', strtotime($timestring))

Converting string to Date and DateTime

复制自:https://stackoverflow.com/a/6239010/2478144

  

不要在strtotime函数中使用正斜杠/和连字符之间有区别。从php.net引用

答案 3 :(得分:0)

我认为最好在Mysql本身做到这一点。当我们有这么多功能。如下所示:

SELECT STR_TO_DATE('Jan-31-2014', '%b-%d-%Y');

您可以将其用于插入查询,如下所示:

Insert into tableName `date` = STR_TO_DATE(InputVal, '%b-%d-%Y');

了解更多信息:http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date