All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

PHP Get Month From Date

Last Updated : Mar 11, 2024

PHP Get Month From Date

In this tutorial we will show you the solution of PHP get month from date, here we shown two types of example to get current month using date() method.

The date() method can let you know date and time related information based on the formatting characters it takes in its first parameter. The function can take maximum of two parameters.

If you use only one parameter, it will return information related to current time.

Step By Step Guide On PHP Get Month From Date :-

Here we displayed current month in two different ways using date() method with single parameter of meter ‘m or M’.

The parameter ‘m’ will returns a month as a number with leading zero like 04,01 and second type ‘M’ parameter will returns a month as short text of three letter like Jun,Apr.

These two types date of month printed using echo method as we know in php this is used to displays on webpage.

We can also get current month using Date Time class and for that we need to create an object of DateTime() class then use format() method of DateTime() class to retrieve the month from newly created object.

echo "The current Month : ". date("m");
echo "<br>The string format current Month : ". date("M");
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. In first line we printed numeric type of month on webpage using date() method with parameter ‘m’ with the string message ‘The current Month :’.
  4. Same as above point we printed text type of three letter result of month using date() method with parameter ‘M’ with string string ‘The string format current Month :’.
  5. Both are displayed on webpage using echo() method. The echo method is used to display the output of parameters that are passed to it.
  6. It displays the outputs of one or more strings separated by commas. The print accepts one argument at a time & cannot be used as a variable function in PHP.
  7. Before execute program on browser needs to confirm one thing whether we starts the xampp server or not because it leads us fail to display result.

Conclusion :-

In conclusion we are able to know how to get month from date using php.

When work with php we need to create and process php files at server location and then we need to start the server before execute the program.

When we executing this program on browser it will prints result of ‘The current Month : 08, The string format current Month : Aug’.

We can also set some date on php variable then we can get month only from the date using strtotime() method later we will discuss this in detail.

I hope this tutorial on PHP get month from date helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪