开发者

Creating date in PHP

开发者 https://www.devze.com 2023-02-23 14:27 出处:网络
I need to convert this date: 10.04开发者_开发技巧.2011 19:00 To a date variable that I can use in PHP.

I need to convert this date:

10.04开发者_开发技巧.2011 19:00

To a date variable that I can use in PHP.

Can someone help me with that? I tried this way:

$dateConverted = date("d.m.Y H:i",strtotime ($date));

But it returns 01.01.1970 00:00


DateTime::createFromFormat() to the rescue!

It looks like your format is d.m.Y H:i.

So, this should work for you:

$dt = DateTime::createFromFormat('d.m.Y H:i', '10.04.2011 19:00');
echo $dt->format('Y-m-d H:i:s');

You should also take a look at the formats that strtotime and DateTime operate on. In particular, the reason that date didn't parse in strtotime is that it only expects dots as delimiters between Y, M and D if the year is only two digits. That's an odd one, don't look at me, it's not my fault.

0

精彩评论

暂无评论...
验证码 换一张
取 消