php 将日期转换为 MySQL 日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1814589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php convert date to MySQL date
提问by minimalpop
I need to convert a date in this format:
我需要以这种格式转换日期:
November 28, 2009
2009 年 11 月 28 日
to a MySQL date format:
到 MySQL 日期格式:
2009-28-11
2009-28-11
What's the best method to convert the date using PHP?
使用 PHP 转换日期的最佳方法是什么?
回答by mauris
Improvised from: http://www.bigroom.co.uk/blog/dates-in-php-and-mysql
即兴自:http: //www.bigroom.co.uk/blog/dates-in-php-and-mysql
$mysqldate = date( 'Y-m-d', strtotime( $phpdate ) );
// Example:
$phpdate = 'November 20, 2009';
$mysqldate = date( 'Y-m-d', strtotime( $phpdate ) );
echo $mysqldate;
// output: 2009-11-20
回答by desloovere_j
If you want a "date" to be converted to "DateTime" this is the best way =
如果您想将“日期”转换为“日期时间”,这是最好的方法=
// for example: you have a string with the following
$dateFormat = 'd/m/Y';
$dateString = '02/12/2019';
// you can easily create DateTime using
$dateTime = \DateTime::createFromFormat($dateFormat, $dateString);
As described in the php documentation for createFromFormat.
And to answer completely on your question:
并完全回答你的问题:
echo $dateTime->format('y-m-d');
回答by antik
回答by Sumit
There are two options you can use which are strtotimeor preg_splitand sprintf. I recommend you use strtotime. The structure goes like this:
您可以使用两个选项,分别是strtotime或preg_split和sprintf。我建议你使用strtotime. 结构是这样的:
$date = 'November 28 2009';
$sqldate = date('Y-m-d', strtotime($date));
Make sure the Y is capital so it reads as 0000 otherwise it will read 00.
确保 Y 是大写的,所以它读为 0000 否则它会读为 00。

