为什么date()不能正确地将YYMMDDHHMM转换为MySQL可接受的日期格式? [英] Why does date() not convert YYMMDDHHMM to MySQL acceptable date format correctly?
本文介绍了为什么date()不能正确地将YYMMDDHHMM转换为MySQL可接受的日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要一个字符串:
1511030830 // YYMMDDHHMM
并创建一个MySQL时间戳,如:
And create a MySQL timestamp like:
2015-11-03 08:30:00
但是,当尝试这样做将无法正常工作: p>
However when try this it will not work:
$string='1511030830';
$date=date("Y-m-d H:i:s", $string);
var_dump($date);exit;
由于上述输出:
string(19) "1970-01-01 02:00:00"
我正在使用PHP版本5.5.30
I am using PHP version 5.5.30
php.ini中是否有某种设置会影响到这一点?
Is there some kind of settings in php.ini that would affect this?
推荐答案
您可以使用DateTime :: createFromFormat:
You can use DateTime::createFromFormat:
<?php
$date = DateTime::createFromFormat('ymdHi', '1511030830');
var_dump($date->format("Y-m-d H:i:s"));
应该做你所需要的。
http://php.net/manual/en/datetime.createfromformat.php
这篇关于为什么date()不能正确地将YYMMDDHHMM转换为MySQL可接受的日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文