How to parse a date string in PHP?

后端 未结 2 1661
执笔经年
执笔经年 2020-12-01 18:23

With a date string of Apr 30, 2010, how can I parse the string into 2010-04-30 using PHP?

相关标签:
2条回答
  • 2020-12-01 19:01

    Either with the DateTime API (requires PHP 5.3+):

    $dateTime = DateTime::createFromFormat('F d, Y', 'Apr 30, 2010');
    echo $dateTime->format('Y-m-d');
    

    or the same in procedural style (requires PHP 5.3+):

    $dateTime = date_create_from_format('F d, Y', 'Apr 30, 2010');
    echo date_format($dateTime, 'Y-m-d');
    

    or classic (requires PHP4+):

    $dateTime = strtotime('Apr 30, 2010');
    echo date('Y-m-d', $dateTime);
    
    0 讨论(0)
  • 2020-12-01 19:06

    Try http://php.net/manual/en/function.strtotime.php to convert to a timestamp and then http://www.php.net/manual/en/function.date.php to get it in your own format.

    0 讨论(0)
提交回复
热议问题