Problem:
You have a date which may consist of day, month, and year. Now, You just want to retrieve only the month from that date.
Solution:
There are few different ways to retrieve month part from a date. In the following, you’ll learn methods to retrieve month portion from current date or from any date–
Method 1: Using date() function to retrieve current month
PHP’s date() function 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.
You can use three different formatting characters in the first parameter of the date() function to get three different forms of a month. These formatting characters are-
- m- To represent a month as a number with leading zero. Ex. 01, 12
- M – To represent a month as short text of three letters. Ex. Jun
- n – To represent a month as a number without leading zero. Ex. 1, 12
See the formatting characters in action in the example below-
<?php echo "The 2 digit representation of current month with leading zero is: " . date("m"); echo "<br />"; echo "The textual representation of current month with leading zero is: " . date("M"); echo "<br />"; echo "The 2 representation digit of current month without leading zero is: " . date("n"); ?>
[wpdm_file id=158]
Output:
The 2 digit representation of current month with leading zero is: 06
The textual representation of current month with leading zero is: Jun
The 2 representation digit of current month without leading zero is: 6
Method 2: Using strtotime() and date() function to retrieve month from any date
Using strtotime() function, to get the month from any date, we’ll follow two steps-
- First convert a date to its equivalent timestamp. A timestamp of a date is the representation of seconds from January 1 1970 00:00:00 UTC to that time, then,
- Use the date() function and the formatting character to retrieve month from that timestamp.
Now, see it in action in the following example-
<?php $timestamp = strtotime("5th September 2003"); echo "The 2 digit representation of current month with leading zero is: " . date("m", $timestamp); echo "<br />"; echo "The textual representation of current month with leading zero is: " . date("M", $timestamp); echo "<br />"; echo "The 2 digit representation of current month without leading zero is: " . date("n", $timestamp); ?>
[wpdm_file id=159]
Output:
The 2 digit representation of current month with leading zero is: 09
The textual representation of current month with leading zero is: Sep
The 2 digit representation of current month without leading zero is: 9
Method 3: Using DateTime class to get current month
From PHP 5.2, PHP provides some ready-made classes to help developers to solve daily problems they face. One of those classes is DateTime class which solves date time related issues.
To get current month using DateTime class, follow the two steps-
- Create an object of DateTime() class. When you use DateTIme() class without any parameter, it represents the current time.
- Then, use format() method of the DateTime() class to retrieve the year from the newly created object.
See the following example-
<?php $now = new DateTime(); echo "The 2 digit representation of current month with leading zero is: " . $now->format('m'); echo "<br />"; echo "The textual representation of current month with leading zero is: " . $now->format('M'); echo "<br />"; echo "The 2 digit representation of current month without leading zero is: " . $now->format('n'); ?>
[wpdm_file id=160]
Output:
The 2 digit representation of current month with leading zero is: 06
The textual representation of current month with leading zero is: Jun
The 2 digit representation of current month without leading zero is: 6
Method 4: Using CreateFromFormat() method to get month from any date
In this method, we’ll retrieve month from any date in the following two steps-
- First, create a DateTime object from the createFromFormat() method of DateTime class using your supplied date, then
- Retrieve month from that object using format() method mentioning formatting parameter “m”, “M”, or “n” in the method’s parameter.
See it in action in the following example –
<?php $dateObj = DateTime::createFromFormat('jS F Y', '5th February 1993'); echo "The 2 digit representation of current month with leading zero is: " . $dateObj->format('m'); echo "<br />"; echo "The textual representation of current month with leading zero is: " . $dateObj->format('M'); echo "<br />"; echo "The 2 digit representation of current month without leading zero is: " . $dateObj->format('n'); ?>
[wpdm_file id=161]
Output:
The 2 digit representation of current month with leading zero is: 02
The textual representation of current month with leading zero is: Feb
The 2 digit representation of current month without leading zero is: 2