Putting a comma in a string

I have this date string:
29 July 2016

and I want to put a comma right after July making it like this 29 July, 2016.

My thoughts are to use str_replace but I do not know if it is the right choice for this occasion.

Where do you get the values for the date?

Php has an extensive date(); function which may be useful.


the values come from the database…
I use DateTime class and the format method just change the format.

And now I must add a comma also after the month.

So just add the comma into your format string? “j F, Y”

One method…

Try converting the string using strtotime(); which will return a UNIX time stamp. The result is ideal for the Php date(); function.

Looking forward to alternative methods :slight_smile:

Many thanks.I really do not know why I did not think of that earlier…

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.