php

Convert String date to DATE MONTH-NAME YEAR format php

To convert a string to a formatted date you can use the date() method in PHP

<?php
    $date_str = "10-01-2021";
    $full_date= strtotime($date_str); 
    echo date('d F Y', $full_date); 
    //PRINT- 10 January 2021
?>
Output
10 January 2021

In the code snippet, we want to convert our date string '10-01-2021' to '10 January 2021'. To do this we use strtotime() method of PHP to convert the date string and then pass its value to the PHP date() function.

You can change 'd F Y' value according to your requirement. Like if you only want month name and year you can use

<?php
    echo date('F Y', $full_date);
    //PRINTS- January 2021
?>
<?php 
    $d = date_create('DATE_STRING'); 
    echo date_format($d,"F j\, Y"); 

    // -> March 10, 2022
?>
You can also use the above PHP code example to convert a Date String to the Format - "Month_Name Date, Year" format.
Was this helpful?