Php Can';设置日期时间
我有这样一个日期:Php Can';设置日期时间,php,mysql,database,date,datetime,Php,Mysql,Database,Date,Datetime,我有这样一个日期:20-03-2016 10:28:24.8402750 我的数据库上的字段是一个DateTime。当我尝试插入上面日期的记录时,我得到:0000-00-00 00:00:00 我还尝试了时间戳和同样的问题。我做错了什么 更新: 这是我生成日期的代码: $now = new DateTime(null, new DateTimeZone('Europe/Rome')); $now->getTimezone(); $result = $now->format('d-m-
20-03-2016 10:28:24.8402750
我的数据库上的字段是一个DateTime
。当我尝试插入上面日期的记录时,我得到:0000-00-00 00:00:00
我还尝试了时间戳
和同样的问题。我做错了什么
更新:
这是我生成日期的代码:
$now = new DateTime(null, new DateTimeZone('Europe/Rome'));
$now->getTimezone();
$result = $now->format('d-m-Y H:i:s') . substr((string)microtime(), 1, 8);
return $result;
您应该使用默认格式的
DateTime
值:
2016-03-20 10:28:24
选择时间戳后,对当前时间戳使用默认值。然后它将工作只需将格式字符串
d-m-Y H:i:s
更改为Y-m-d H:i:s
,代码如下所示:
$result = $now->format('Y-m-d H:i:s');
并删除不需要的毫秒数 发布您尝试过的内容。@TungD.Nguyen检查我的更新…为什么要这样做
。substr((字符串)microtime(),1,8)
?@itzmukeshy7仅用于获取微时间。。但是我不需要更多这个。。