将日期时间转换为字符串 PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10569053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:29:10  来源:igfitidea点击:

Convert DateTime to String PHP

phpdatetime

提问by Netorica

I have already researched a lot of site on how can I convert PHP DateTime object to String. I always see "String to DateTime" and not "DateTime to String"

我已经研究了很多关于如何将 PHP DateTime 对象转换为 String 的网站。我总是看到“字符串到日期时间”而不是“日期时间到字符串”

PHP DateTime can be echoed, but what i want to process my DateTime with PHP string functions.

PHP DateTime 可以回显,但我想用 PHP 字符串函数处理我的 DateTime。

My question is how can I make PHP dateTime Object to a string starting from this kind of code:

我的问题是如何将 PHP dateTime 对象设置为从这种代码开始的字符串:

$dts = new DateTime(); //this returns the current date time
echo strlen($dts);

回答by rjz

You can use the formatmethod of the DateTimeclass:

您可以使用该类的format方法DateTime

$date = new DateTime('2000-01-01');
$result = $date->format('Y-m-d H:i:s');

If formatfails for some reason, it will return FALSE. In some applications, it might make sense to handle the failing case:

如果format由于某种原因失败,它将返回FALSE。在某些应用程序中,处理失败的情况可能是有意义的:

if ($result) {
  echo $result;
} else { // format failed
  echo "Unknown Time";
}

回答by sailer

The simplest way I found is:

我发现的最简单的方法是:

$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;

I hope it helps.

我希望它有帮助。

回答by Glauco Neves

There are some predefined formats in date_d.phpto use with formatlike:

有一些预定义的格式date_d.php可以使用,format例如:

define ('DATE_ATOM', "Y-m-d\TH:i:sP");
define ('DATE_COOKIE', "l, d-M-y H:i:s T");
define ('DATE_ISO8601', "Y-m-d\TH:i:sO");
define ('DATE_RFC822', "D, d M y H:i:s O");
define ('DATE_RFC850', "l, d-M-y H:i:s T");
define ('DATE_RFC1036', "D, d M y H:i:s O");
define ('DATE_RFC1123', "D, d M Y H:i:s O");
define ('DATE_RFC2822', "D, d M Y H:i:s O");
define ('DATE_RFC3339', "Y-m-d\TH:i:sP");
define ('DATE_RSS', "D, d M Y H:i:s O");
define ('DATE_W3C', "Y-m-d\TH:i:sP");

Use like this:

像这样使用:

$date = new \DateTime();
$string = $date->format(DATE_RFC2822);

回答by Dhawal Naik

echo date_format($date,"Y/m/d H:i:s");

回答by sumit sharma

Its worked for me

它对我有用

$start_time   = date_create_from_format('Y-m-d H:i:s', $start_time);
$current_date = new DateTime();
$diff         = $start_time->diff($current_date);
$aa           = (string)$diff->format('%R%a');
echo gettype($aa);

回答by lisandro

Shorter way using list. And you can do what you want with each date component.

使用列表的更短方法。您可以对每个日期组件执行您想要的操作。

list($day,$month,$year,$hour,$min,$sec) = explode("/",date('d/m/Y/h/i/s'));
echo $month.'/'.$day.'/'.$year.' '.$hour.':'.$min.':'.$sec;