将DateTime转换为String PHP [英] Convert DateTime to String PHP

查看:97
本文介绍了将DateTime转换为String PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我已经研究了很多网站,我如何将PHP DateTime对象转换为String。
我总是看到String to DateTime而不是DateTime to String



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



现在我的问题是如何使PHP dateTime对象成为字符串
从这种代码开始

 <?php 
$ dts = new DateTime(); //这返回当前日期时间
echo strlen($ dts);
?>


解决方案

您可以使用 格式 方法的 DateTime class:

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

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

  if($ result){
echo $结果;
} else {// format failed
echoUnknown Time;
}


ok, I 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 can be echoed, but what i want to process my DateTime with PHP string functions.

Now my question is how can I make PHP dateTime Object to a string starting from this kind of codes

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

解决方案

You can use the format method of the DateTime class:

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

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

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

这篇关于将DateTime转换为String PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆