【问题标题】:convert date to Mysql date format php将日期转换为Mysql日期格式php
【发布时间】:2013-04-22 04:58:09
【问题描述】:

我的数据库中有这种日期格式

19th April 2013

我想把它转换成

2013-04-19

我用过if,else条件,但是太长太复杂了。有没有内置函数??

【问题讨论】:

    标签: php mysql date


    【解决方案1】:

    使用PHP'sdate函数

    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
      

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          【解决方案4】:

          以下 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']));
          

          【讨论】:

            猜你喜欢
            • 2011-08-13
            • 2011-10-11
            • 1970-01-01
            • 2012-06-05
            • 1970-01-01
            • 2014-02-25
            • 1970-01-01
            • 2018-02-12
            • 2013-12-15
            相关资源
            最近更新 更多