将日期转换为Mysql日期格式PHP

3

我在数据库中有这个日期格式

19th April 2013

我想将它转换为
2013-04-19

我使用了if,else条件语句,但它变得太长且复杂。是否有任何内置函数可以使用?

4个回答

8
使用 PHP 的 date 函数。
date("Y-m-d",strtotime("19th April 2013"));

2
是的,以下代码可以更改日期格式。
date("Y-m-d",strtotime("19th April 2013")); to get as 2013-04-19

对于第二个问题

    date("Y/m/d",strtotime("17th April 2013")); to get as 2013-04-17

1

您仍然可以通过查询来执行此操作,因此不会在PHP端进行任何转换。由于列的数据类型为字符串,您需要使用STR_TO_DATE将其转换为有效日期。

SELECT DATE_FORMAT(STR_TO_DATE(columnname, '%D %M %Y'), '%Y-%m-%d') new_format
FROM   TableName

1
以下PHP代码将日期转换为所需格式。
 $date = '19th April 2013'
 echo date('Y-m-d', strtotime($date));

其他格式如下:
April 22, 2013  date("F d, Y",strtotime($myrow['date']));
Monday, April 22, 2013  date("l, F d, Y",strtotime($myrow['date']));
Apr 22, 2013    date("M d, Y",strtotime($myrow['date']));
22 April 2013   date("d F Y",strtotime($myrow['date']));
22 Apr 2013 date("d M Y",strtotime($myrow['date']));
Mon, 22 Apr 2013    date("D, d M Y",strtotime($myrow['date']));
Monday, the 22nd of April, 2013 date("l",strtotime($myrow['date'])) . ", the " .   date("jS",strtotime($myrow['date'])) . " of " . date("F, Y",strtotime($myrow['date']));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接