简体   繁体   English

用PHP内联更改日期格式的问题

[英]Issue with changing date format inline with PHP

I am pulling a date from a database and it is in the format 2017-12-07. 我正在从数据库中提取日期,其格式为2017-12-07。 That is how I was displaying the date, however, I would like to display is like Dec 7, 17. 那就是我显示日期的方式,但是,我想显示的就像12月7日,17日。

What I tried. 我试过了

echo "<td>" . date_format($row['returndate'],'M d, y') . "</td>";

My results end up empty. 我的结果最终是空的。 I have successfully changed the date format with in the MYSQLI line, however, I was wanting to do it with PHP. 我已经成功地在MYSQLI行中更改了日期格式,但是,我想使用PHP进行更改。

UPDATE 更新

I apologize, it seems I over looked something simple. 我很抱歉,看来我看起来过分简单。 Below fixed it. 下面修复它。

date('M d, y', strtotime($row['returndate']))

I guess you have to do strtotime before date. 我想你必须在约会之前做strtotime。

Try this 尝试这个

echo "<td>" . date('M d, y', strtotime($row['returndate'])) . "</td>";

Output 输出量

Dec 07, 17 

date_format requires an DateTime object. date_format需要一个DateTime对象。 In your case, you can simply use (without class DateTime ): 就您而言,您可以简单地使用(不使用DateTime类):

print date('M d, y', strtotime('2017-12-07'));
Dec 07, 17

Or if you would like to use DateTime exactly, you can try: 或者,如果您想完全使用DateTime ,可以尝试:

php > $d = new DateTime('2017-12-07');
php > print $d->format('M d, y');
Dec 07, 17

You should use the php DateTime class: 您应该使用php DateTime类:

$date = new DateTime($row['returndate']);
echo $date->format('M d, y');

You can directly pass the date , first create date object and then pass it date_format For eg: 您可以直接传递date,首先创建date对象,然后再传递date_format例如:

$date = date_create($row['returndate']);
echo "<td>" . date_format($date,'M d, y') . "</td>";

Output 输出量

Dec 07, 17

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM