format a date in to the format month day, year in php

You can use the date() function in PHP to format a date string into any desired format. To format a date into "month day, year" format, you can use the following code:

main.php
$date_str = "2022-06-30"; // Your date string in yyyy-mm-dd format
$date = new DateTime($date_str);
$formatted_date = $date->format('F j, Y'); // Format the date string into month day, year format
echo $formatted_date; // Output: June 30, 2022
244 chars
5 lines

In the above code, we first create a DateTime object from the original date string. Then, we use the format() method to format the date string into the desired format "F j, Y", where "F" represents the full month name, "j" represents the day of the month without leading zeros, and "Y" represents the year in four digits. Finally, we print out the formatted date using the echo statement.

gistlibby LogSnag