how to get month name from date in PHP?

I am explaining you, we will learn how to get the month name from the date in PHP. The PHP provides very useful functions by using it we can easily format the date.

Example 1: Extract Month Name From Date

In this example, we have used the createFromFormat() function which helps us to provide month name from the specific date.

<?php 
  $dateTime = DateTime::createFromFormat('d/m/Y', '10/04/2005');
  echo $dateTime->format('F'); //April
  echo "<br/>";
  echo $dateTime->format('M'); //Apr
?>

Result:

April
Apr

Example 2: Extract Month Name Using date() Function

I am explaining you, If you don’t want to use the DateTime class then you can use the PHP’s date() function for get the month name from date.

<?php
    // Date Format: Y/m/d
    $CustomDate = '2015/01/04';;
    echo $month = date('F', strtotime($CustomDate));//January
    echo "<br/>";
    echo $month = date('M', strtotime($CustomDate));//Jan

?>

Result:

January
Jan

Leave a Reply

Your email address will not be published. Required fields are marked *

23 + = 27