문제

I am sure this must be a common question but I cannot find it anywhere so here it goes:

I have dates in this format: 09/Jul/2003:00:03:48 -0300 and I want to push them into a DATETIME field in MySQL.

What is the easiest way to do this? Pushing them in as is results in them being zeroed.

도움이 되었습니까?

해결책

MySQL doesn't include the timezone in DATETIMEs, but you can use:

STR_TO_DATE('09/Jul/2003:00:03:48', '%d/%b/%Y:%H:%i:%s')

...to convert a string representation of a date/time into a DATETIME for storing in the database.

Reference:

다른 팁

As far as I know can not be changed and has to be in the format below:

'YYYY-MM-DD HH:MM:SS'

http://dev.mysql.com/doc/refman/5.1/en/datetime.html

My favorite function in all of PHP is strtotime(). If that can't interpret the input then I probably can't code something that could.

So I would do something like:

mysql_real_escape_string(date('Y-m-d H:i:s', strtotime($stragely_formatted_date)));

to prepare a DATE string to give to MySQL.

As always, think about timezones in your the design.

The mysql_real_escape_string() is probably unnecessary but I always feel more secure when I use it.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top