Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将日期格式从Y-m-d H:i:s转换为dmy_Php_Date_Datetime - Fatal编程技术网

Php 将日期格式从Y-m-d H:i:s转换为dmy

Php 将日期格式从Y-m-d H:i:s转换为dmy,php,date,datetime,Php,Date,Datetime,我有一个日期时间字符串,格式为Y-m-d H:I:s,如下所示: $dateTime = '2018-07-06 18:53:21'; $convertedDateTime = $this->convertDateTime($dateTime); echo $convertedDateTime; echo date_format($date,"Y/m/d H:i:s"); //e.g. 2013/03/15 00:00:00 echo date_format($date,"dmy"

我有一个日期时间字符串,格式为
Y-m-d H:I:s
,如下所示:

$dateTime = '2018-07-06 18:53:21';
$convertedDateTime = $this->convertDateTime($dateTime);
echo $convertedDateTime; 
echo date_format($date,"Y/m/d H:i:s");  //e.g. 2013/03/15 00:00:00
echo date_format($date,"dmy");   //e.g. 150313    -- Y capital would return 2013
我想将其转换为dmy格式,如下所示:

$dateTime = '2018-07-06 18:53:21';
$convertedDateTime = $this->convertDateTime($dateTime);
echo $convertedDateTime; 
echo date_format($date,"Y/m/d H:i:s");  //e.g. 2013/03/15 00:00:00
echo date_format($date,"dmy");   //e.g. 150313    -- Y capital would return 2013

我从上面的
echo
中预期的结果是060718,我如何实现这一点?

您可以使用
date\u格式
如下:

$dateTime = '2018-07-06 18:53:21';
$convertedDateTime = $this->convertDateTime($dateTime);
echo $convertedDateTime; 
echo date_format($date,"Y/m/d H:i:s");  //e.g. 2013/03/15 00:00:00
echo date_format($date,"dmy");   //e.g. 150313    -- Y capital would return 2013

您可以像这样使用
date\u格式

$dateTime = '2018-07-06 18:53:21';
$convertedDateTime = $this->convertDateTime($dateTime);
echo $convertedDateTime; 
echo date_format($date,"Y/m/d H:i:s");  //e.g. 2013/03/15 00:00:00
echo date_format($date,"dmy");   //e.g. 150313    -- Y capital would return 2013

有两种方法可供使用:

日期
-

用法示例:

$myDate = '2018-07-06 09:49:00';
$myDate = date('d-m-y', strtotime($myDate));
但通常情况下,您会希望使用DateTime:


有两种方法可供使用:

日期
-

用法示例:

$myDate = '2018-07-06 09:49:00';
$myDate = date('d-m-y', strtotime($myDate));
但通常情况下,您会希望使用DateTime:


啊,是的,忘记了date_格式-又好又干净:)得到了以下错误:
警告:date_格式()期望参数1是DateTimeInterface,在
中给出的字符串是传递字符串而不是日期,在您的情况下,您将传递$convertedDateTime作为参数是的,忘记了date_格式-漂亮干净:)得到了这个错误:
警告:date_格式()期望参数1是DateTimeInterface,在
中给出的字符串是传递字符串而不是日期,在您的情况下,您将传递$convertedDateTime作为参数,只需使用
date_格式($date,'dmy')只需使用
date_格式($date,'dmy')