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.

What is 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.

date_parse_from_format(format, date)

Detail Information regarding syntax.

formatIt is required. It represents the format
dateIt is required. A String that specifies a date.
date parse format information

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.

What is 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.

date(format, timestamp)

Some important information regarding syntax.

formatIt is Required. It represents the format outputted date string.
timestampIt is Optional. It represents an integer Unix timestamp.
date parameter information

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

What is explode() function?

It is a function which is used to break the string into array.

Syntax for explode

separatorIt is required. Represent where to break the string
stringIt is required. A string which is to split
limitIt is optional.
explode syntax information

Using the below code you can get month.

<?php$date_variable = "2010-08-12";$parts = explode('-',$date_variable);echo $month = $parts[1];//OUTPUT// 08?>

Remove ampersand from String PHP


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.