-1

I have read many ways to do this when the date is a simple variable, but how would I format the date to read "M, j" meaning "May, 5" when my php statement is the following?

echo "$row->nick $row->lastname, ($row->updated_at)";

that now outputs this:

John Smith, (2017-05-10 12:18:12)

I want it to read:

John Smith, (May 10)

Thanks

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
user2554201
  • 33
  • 1
  • 8

3 Answers3

2

You can use the php date() function:

echo date('M j', strtotime($row->updated_at));
Derenir
  • 537
  • 1
  • 12
  • 29
1

Apparently $row->updated_at is now a string. You can change it into a DateTime object;

$updated_at = new DateTime($row->updated_at);

Now you're able to format it the way you want.

echo $updated_at->format('M j');
Qirel
  • 25,449
  • 7
  • 45
  • 62
Paul
  • 79
  • 1
  • 7
  • this seemed to work but all of the dates were the same instead of being different 'foreach', I presume from making "updated_at" a global variable – user2554201 May 10 '17 at 14:04
  • Make sure you put the $updated_at = inside the foreach loop. It'll create a new DateTime object for every row, so they won't be the same. – Paul May 10 '17 at 14:06
-1

you can use DateTime::format()

$date = new DateTime($row->updated_at);
echo "$row->nick $row->lastname, ($date->format('M j'))";
Qirel
  • 25,449
  • 7
  • 45
  • 62
Sugumar Venkatesan
  • 4,019
  • 8
  • 46
  • 77
  • If you want to remove the error `Undefined property: DateTime::$format` then try `echo "$row->nick $row->lastname, ({$date->format('M j')})";` – RiggsFolly May 10 '17 at 13:51