Why does date() not convert YYMMDDHHMM to MySQL acceptable date format correctly?

丶灬走出姿态 提交于 2019-12-24 14:09:14

问题


I would like to take a string like:

1511030830 // YYMMDDHHMM

And create a MySQL timestamp like:

2015-11-03 08:30:00

However when try this it will not work:

$string='1511030830';
$date=date("Y-m-d H:i:s", $string);
var_dump($date);exit;

Because the above outputs:

string(19) "1970-01-01 02:00:00"

I am using PHP version 5.5.30

Is there some kind of settings in php.ini that would affect this?


回答1:


You can use DateTime::createFromFormat:

<?php
$date = DateTime::createFromFormat('ymdHi', '1511030830');
var_dump($date->format("Y-m-d H:i:s"));

That should do what you need.

http://php.net/manual/en/datetime.createfromformat.php




回答2:


Simply use DateTime class of PHP like as

$date = DateTime::createFromFormat("ydmhi","1511030830");
echo $date->format('Y-m-d H:i:s') . "\n";

Demo




回答3:


use MySQL str_to_date

SELECT str_to_date(1511030830,'%y%d%m')



回答4:


Please check your php version once. My current version in server 5.5.18 and in local 5.6.11

Im using your code only. Its working fine

$string='1511030830';
$date=date("Y-m-d H:i:s", $string);
var_dump($date);exit;

Output:

string(19) "2017-11-18 10:47:10"

See demo here

$timestamp = 1511030830;
echo gmdate("Y-m-d H:i:s", $timestamp);//2017-11-18 18:47:10

Demo for gmdate() here



来源:https://stackoverflow.com/questions/33492868/why-does-date-not-convert-yymmddhhmm-to-mysql-acceptable-date-format-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!