php 将日期字符串转换为 mysql 日期时间字段

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

convert date string to mysql datetime field

phpmysql

提问by mjr

I have a bunch of records with dates formatted as a string such as '04/17/2009'

我有一堆日期格式为字符串的记录,例如“04/17/2009”

I want to convert them to a mysql datetime field

我想将它们转换为 mysql 日期时间字段

I plan to use a foreach loop to read the old date value and insert the newly formatted value into a new field in each record

我计划使用 foreach 循环读取旧的日期值并将新格式化的值插入到每条记录的新字段中

what would be the best way to convert that string...I thought php might have a way to do it automatically?

转换该字符串的最佳方法是什么...我认为 php 可能有一种方法可以自动执行此操作?

thanks

谢谢

回答by Pekka

First, convert the string into a timestamp:

首先,将字符串转换为时间戳:

$timestamp = strtotime($string);

Then do a

然后做一个

date("Y-m-d H:i:s", $timestamp);

回答by goat

If these strings are currently in the db, you can skip php by using mysql's STR_TO_DATE()function.

如果这些字符串当前在数据库中,您可以使用 mysql 的STR_TO_DATE()函数跳过 php 。

I assume the strings use a format like month/day/yearwhere monthand dayare always 2 digits, and yearis 4 digits.

我假设字符串使用类似month/day/yearwhere的格式,month并且day总是 2 位数字,并且year是 4 位数字。

UPDATE some_table
   SET new_column = STR_TO_DATE(old_column, '%m/%d/%Y')

You can support other date formats by using other format specifiers.

您可以通过使用其他格式说明符来支持其他日期格式。

回答by Kethryweryn

Use DateTime::createFromFormat like this :

像这样使用 DateTime::createFromFormat :

$date = DateTime::createFromFormat('m/d/Y H:i:s', $input_string.' 00:00:00');
$mysql_date_string = $date->format('Y-m-d H:i:s');

You can adapt this to any input format, whereas strtotime() will assume you're using the US date format if you use /, even if you're not.

您可以将其调整为任何输入格式,而 strtotime() 会假设您使用美国日期格式(如果您使用 /,即使您没有使用)。

The added 00:00:00 is because createFromFormat will use the current date to fill missing data, ie : it will take the current hour:min:sec and not 00:00:00 if you don't precise it.

添加的 00:00:00 是因为 createFromFormat 将使用当前日期来填充缺失的数据,即:如果您不精确,它将使用当前的小时:分钟:秒而不是 00:00:00。

回答by Tyler Carter

$time = strtotime($oldtime);

Then use date()to put it into the correct format.

然后用date()把它变成正确的格式。

回答by SDGuero

I assume we are talking about doing this in Bash?

我假设我们正在谈论在 Bash 中执行此操作?

I like to use sed to load the date values into an array so I can break down each field and do whatever I want with it. The following example assumes and input format of mm/dd/yyyy...

我喜欢使用 sed 将日期值加载到数组中,这样我就可以分解每个字段并使用它做任何我想做的事情。以下示例假定输入格式为 mm/dd/yyyy...

DATE=
DATE_ARRAY=(`echo $DATE | sed -e 's/\// /g'`)
MONTH=(`echo ${DATE_ARRAY[0]}`)
DAY=(`echo ${DATE_ARRAY[1]}`)
YEAR=(`echo ${DATE_ARRAY[2]}`)
LOAD_DATE=$YEAR$MONTH$DAY

you also may want to read up on the date command in linux. It can be very useful: http://unixhelp.ed.ac.uk/CGI/man-cgi?date

您可能还想阅读 Linux 中的 date 命令。它可能非常有用:http: //unixhelp.ed.ac.uk/CGI/man-cgi?date

Hope that helps... :)

希望有帮助... :)

-Ryan

-瑞安

回答by vpgodara

SELECT *
FROM table_name
WHERE CONCAT( SUBSTRING(json_date, 11, 4 ) ,  '-', SUBSTRING( json_date, 7, 2 ) ,  '-', SUBSTRING( json_date, 3, 2 ) ) >= NOW();

json_date ["05/11/2011"]

json_date ["05/11/2011"]