我已经研究了很多关于如何将PHP DateTime对象转换为字符串的站点。我总是看到"String to DateTime“而不是"DateTime to String”
PHP DateTime可以回显,但我想用PHP字符串函数来处理我的DateTime。
我的问题是,如何将PHP dateTime对象转换为从这种代码开始的字符串:
$dts = new DateTime(); //this returns the current date time
echo strlen($dts);
发布于 2012-05-13 11:17:41
您可以使用DateTime
类的format
方法:
$date = new DateTime('2000-01-01');
$result = $date->format('Y-m-d H:i:s');
如果format
由于某种原因失败,它将返回FALSE
。在一些应用程序中,处理失败的情况可能是有意义的:
if ($result) {
echo $result;
} else { // format failed
echo "Unknown Time";
}
发布于 2013-12-07 20:47:12
我发现的最简单的方法是:
$date = new DateTime(); //this returns the current date time
$result = $date->format('Y-m-d-H-i-s');
echo $result . "<br>";
$krr = explode('-', $result);
$result = implode("", $krr);
echo $result;
我希望它能帮上忙。
发布于 2015-08-13 14:52:03
echo date_format($date,"Y/m/d H:i:s");
https://stackoverflow.com/questions/10569053
复制相似问题