PHP Get Month From Date
Last Updated : Jan 1, 2023
IN - PHP
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.
<?php echo "The current Month : ". date("m"); echo "<br>The string format current Month : ". date("M"); ?>
- A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
- The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
- In first line we printed numeric type of month on webpage using date() method with parameter ‘m’ with the string message ‘The current Month :’.
- 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 :’.
- Both are displayed on webpage using echo() method. The echo method is used to display the output of parameters that are passed to it.
- 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.
- 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.
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.