Get date format like “Y-m-d H:i:s” from a php date

对着背影说爱祢 提交于 2019-12-05 03:24:40

As far as I'm aware, there's no guaranteed way of working backwards. The best way might be to try to match regular expressions against expected well-known formats (e.g. \d{2,4}[-/]\d{2}[-/]\d{2} for "Y-m-d") but I can't think of an easy way to do the matching without using regular expressions. You would also have to check that the parsed format makes sense, and you can't do much about ambiguous dates like 2nd of March 2009, which could be represented as 09/03/02, 2009-03-02, 02/03/09, 03/02/09, or even 09/02/03.

You can convert string date to timestamp with strtotime and do with the timestamp what ever you want. ;)

<?php
$date = "2009-10-16 21:30:45";
$ts   = strtotime($date);
echo date('Y-m-d', $ts);
?>

The PHP date() function formats a timestamp to a more readable date and time. Try this code for you project,

 <?php
    $date=date_create('2014-10-20');
    date_time_set($date,12,20,55);
    echo date_format($date,'Y-m-d H:i:s');
    ?>

I know it's late, but would it be best to do:

$date = date("Y-m-d");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!