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')代码>