0

I am pulling a date from a database and it is in the format 2017-12-07. That is how I was displaying the date, however, I would like to display is like Dec 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.

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.

0

4 Answers 4

2

Try this

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

Output

Dec 07, 17 
1
  • This is how I ended up fixing it. I guess I was typing my edit while you answered. Thank you for the quick response.
    – Alan
    Commented Dec 8, 2017 at 3:52
2

date_format requires an DateTime object. In your case, you can simply use (without class 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:

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

You should use the php DateTime class:

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

You can directly pass the date , first create date object and then pass it date_format For eg:

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

Output

Dec 07, 17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.