PHP: Problem with strtotime

天大地大妈咪最大 提交于 2019-11-28 11:41:55

问题


What's going on with strtotime here?

$today = date('m.d.y H:i', time());
echo strtotime($today);

It does not output anything... What's going on?


回答1:


strtotime can only parse certain formats, not any random assortment of numbers and letters. "m.d.y H:i" is not a format strtotime can parse. You'll need to parse that manually using, for example, strptime.




回答2:


Use DateTime::createFromFormat() if you know source format of date ('m.d.y H:i') in your example

print DateTime::createFromFormat('m.d.y H:i',$date)->getTimestamp()

Manual
DateTime::createFromFormat
DateTime::getTimestamp




回答3:


strtotime works with US dates. Try

$today = date('m/d/y H:i', time());
echo strtotime($today);



回答4:


strtotime() is a function for formatting the date, before it is outputted. It seems like the date is already formated in the date() function, and that you make no attempt to format the date in the second line.

Correct code

$today = date("Y-m-d-H.i");
$datenumber = date('Y-m-d',strtotime($today));
$timenumber = date('H.i',strtotime($today));

You can echo all those variables.



来源:https://stackoverflow.com/questions/6919478/php-problem-with-strtotime

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