How do I convert this to be stored in mysql database with a datetime field?

$phptime = '02/21/2014 9:30 am';
有帮助吗?

解决方案 2

Convert the string to a timestamp, then use the php date function to get it in the format you want.

$datetimeString = date( 'Y-m-d H:i:s', strtotime( $phptime ) );

其他提示

Use DateTime::createFromFormat() to read in and format the date.

$phptime = '02/21/2014 9:30 am';
$dt = DateTime::createFromFormat('m/d/Y g:i a', $phptime);
$mysqltime = $dt->format('Y-m-d H:i:s');

MySQL solution: Use STR_TO_DATE function with the php datetime value as input.

mysql> select str_to_date( '02/21/2014 9:30 am', '%m/%d/%Y %h:%i %p' ) dt;
+---------------------+
| dt                  |
+---------------------+
| 2014-02-21 09:30:00 |
+---------------------+
1 row in set (0.00 sec)

Refer to:

I had the experience that you have to separate the components and rearrange them to the mysql datetime format. I have done this before posting it to php and slice it in jquery.

Try using strtotime

So like this:

strtotime($phptime);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top