In this article, you going to see using PHP get month from date in a step by step in a simple way with example.
If you are looking for this topic then you are at a right place.
Let’s start with the topic of using PHP get month from date.
There any many methods to get month from date using PHP.
Method 1 : Using function called
date_parse_from_format() function ?
It is a function which returns an associative array with detailed data regarding specified or current date that is also according to specified format.
Syntax for this function is given below.
Detail Information regarding syntax.
|format||It is required. It represents the format|
|date||It is required. A String that specifies a date.|
Below is code snippet from which you can understand.
<?php $date = $d = date_parse_from_format("Y-m-d", $date); echo $d["month"]; // OUTPUT: // 8 ?>
Method 2 : Using Date function.
It is a function which formats a local date and time and then it returns a well formatted date string.
Below is the Syntax for the date function.
Some important information regarding syntax.
|format||It is Required. It represents the format outputted date string.|
|timestamp||It is Optional. It represents an integer Unix timestamp.|
Below are the characters which is use in date format as follow.
- d – The day of the month [from 01 to 31].
- D – Textual representation of a day [3 letters].
- m – Numeric representation of a month [from 01 to 12].
- M – Short textual representation of a month [3 letters].
- Y – Four-digit representation of a year
- y – Two digit representation of a year
Below is code snippet with help of which also you can get month.
<?php $date = "2010-10-10"; echo date("m", strtotime($date)) // OUTPUT: // 10 ?>
Using explode function to get month.
Method 3: Using explode function
It is a function which is used to break the string into array.
Syntax for explode
|separator||It is required. Represent where to break the string|
|string||It is required. A string which is to split|
|limit||It is optional.|
Using the below code you can get month.
<?php $date_variable = "2010-08-12"; $parts = explode('-',$date_variable); echo $month = $parts; //OUTPUT // 08 ?>
Finally, we have done with PHP get month from date.
I hope you like this article and if you feel like we missed out on anything, please comment below.