我的数据库中有这种日期格式
19th April 2013
我想把它转换成
2013-04-19
我使用了 if,else 条件,但是它变得太长太复杂了。是否有任何内置函数?
使用PHP's
date
功能
date("Y-m-d",strtotime("19th April 2013"));
是的,下面的代码更改日期格式
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
您仍然可以通过查询来做到这一点,这样您就不会进行任何转换PHP
。由于该列的数据类型是字符串,因此您需要使用 将其转换为有效日期STR_TO_DATE
。
SELECT DATE_FORMAT(STR_TO_DATE(columnname, '%D %M %Y'), '%Y-%m-%d') new_format
FROM TableName
以下 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']));