在 MySQL 中插入日期时间时的 PHP date() 格式 [英] PHP date() format when inserting into datetime in MySQL

查看:26
本文介绍了在 MySQL 中插入日期时间时的 PHP date() 格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想将结果插入到 MySQL datetime 类型的列中,传递给 PHP 中 date() 函数的正确格式是什么?

What is the correct format to pass to the date() function in PHP if I want to insert the result into a MySQL datetime type column?

我一直在尝试 date('Y-M-D G:i:s') 但这只是插入了0000-00-00 00:00:00"每次.

I've been trying date('Y-M-D G:i:s') but that just inserts "0000-00-00 00:00:00" everytime.

推荐答案

问题在于您使用的是 'M''D',它们是文本表示,MySQL 期望格式为 2010-02-06 19:30:13

The problem is that you're using 'M' and 'D', which are a textual representations, MySQL is expecting a numeric representation of the format 2010-02-06 19:30:13

尝试:date('Y-m-d H:i:s') 使用等价的数字.

Try: date('Y-m-d H:i:s') which uses the numeric equivalents.

edit:将G改成H,虽然可能没有影响,但你可能想使用前导0的24小时格式.

edit: switched G to H, though it may not have impact, you probably want to use 24-hour format with leading 0s.

这篇关于在 MySQL 中插入日期时间时的 PHP date() 格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆