php 将 MM/DD/YYYY 格式的日期转换为 MySQL 日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19459119/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:31:41  来源:igfitidea点击:

Convert date in format MM/DD/YYYY to MySQL date

phpmysql

提问by Zac Powell

I have a PHP function that gets passed a date in the format MM/DD/YYYYI need to then convert this so that it can be added to a MySQL field that is of type date

我有一个 PHP 函数,它以MM/DD/YYYY我需要的格式传递日期,然后将其转换为可以添加到类型为 MySQL 的字段中date

How would I go about doing this in PHP?

我将如何在 PHP 中执行此操作?

回答by Mattt

$newvalue = date('Y-m-d', strtotime($originalvalue));

回答by PaulV

MySQL displays the DATE type as 'YYYY-MM-DD',so you could do something like:

MySQL 显示 DATE 类型,'YYYY-MM-DD',因此您可以执行以下操作:

date("Y-m-d",strtotime("10/18/2013"));

date("Y-m-d",strtotime("10/18/2013"));

回答by CreatoR

My variant:

我的变种:

  $mysql_date = date('Y-m-d', strtotime(str_replace('/','-', $value)));
  $mysql_date = date('Y-m-d', strtotime(str_replace('/','-', $value)));

回答by dewil

$date = preg_replace('/(\d{2})\/(\d{2})\/(\d{4})/', '--', $date)