Sort month name in PHP

To sort the month name, first off all we accept an array of month name, then  using PHP build-in function timestamp we will convert it into Unix Timestamp after that we will get Month name in number format and store it into another array, which will be use later on. e.g March will 03, and stored it in another array.

PHP Script to sort month name

<?php

// filename is : sort_month_name.php

// function accept month name list in array format
function getsortedMonth($monthList = [])
{
	// take empty array to store sorted month name	
	$sortedNameList = array();
	
	// take empty array to store month name in numbers
	$monthNumberList = array();
	
	// Iterate the loop till end of month list array and inside this, converted month name in number.
	// Iterate the loop till end of month list array and inside this, converted month name in number.
	// Iterate the loop till end of month list array and inside this, converted month name in number.
	$maxLength = count($monthList);
	for($i = 0; $i < $maxLength; $i++)
	{
		// convert month name to number and store it into $monthNumberList array.
		$monthnumber = date('m',strtotime($monthList[$i]));
		array_push($monthNumberList,$monthnumber);	
	}
	
	// sorted, month number in accending order..e.g. 01,02,03....12
	sort($monthNumberList);
	
	// Iterate the loop till end of month and fetch data one by and converted into month name
	for($j = 0; $j < $maxLength; $j++)
	{	
		// fetch month number
		$monthNum  = $monthNumberList[$j];
	
		// converted month number to month name 
		$dateObj   = DateTime::createFromFormat('!m', $monthNum);
		$monthName = $dateObj->format('F'); 
		
		// store month name in sortedNameList array.
		array_push($sortedNameList,$monthName);			
	}
	
	// return all sorted month name.
	return $sortedNameList;

}

echo"<pre>";
// month name in unsorted formate
$monthList = array('November','April','February','March','December','January');

// call function to sort month and assign it into $month_array variable
$month_array = getsortedMonth($monthList);

// print all month_array
print_r($month_array);
?>
Shorted Month Name
Result : Sorted Month Name

Let’s Understand the Logic

  • First of all we define the function name “getsortedMonth” which accept an array of month
  • Then we define 2 empty array to store the sorted sorted month name and month number
  • Count the length of given month list array
  • Iterate the loop and convert month name in number format and stored it into an array
  • Then sort an array, where all months stored in number format
  • After that, Iterate the loop and convert all month number format into month name and stored it into another format.
  • Finally, Return sorted month name array.

How to change date format ?

The strtotime() function is a built-in function in PHP which is used to convert an English textual date-time to a UNIX timestamp. The function returns the time in Unix timestamp. We can return the English textual date-time in date format using the date() function

Let’s understand by an example

<?php
// filename : date_function.php
// current date format is dd-mm-yyyy
$date_before_change =	"04-07-2020";

// i want to change this using help of timestamp 
$timestamp	=	strtotime($date_before_change);
$date_after_change = date("Y-m-d",$timestamp);

echo "Before change date format : ".$date_before_change;
echo "<br>After change date format : ".$date_after_change;

// Here you can do many operations date using timestamp e.g.

echo "<br/>Only Year : ".date('Y',$timestamp);
echo "<br/>Only Month : ".date('M',$timestamp);
echo "<br/>Only Month in number : ".date('m',$timestamp);
echo "<br/>Complete date with month name : ".date('d-M-Y',$timestamp);

?>
Date  Format change
Date Format change

Use below letter to change date format as per your need.

  • d – The day of the month (from 01 to 31)
  • D – A textual representation of a day (three letters)
  • j – The day of the month without leading zeros (1 to 31)
  • l (lowercase ‘L’) – A full textual representation of a day
  • N – The ISO-8601 numeric representation of a day (1 for Monday, 7 for Sunday)
  • S – The English ordinal suffix for the day of the month (2 characters st, nd, rd or th. Works well with j)
  • w – A numeric representation of the day (0 for Sunday, 6 for Saturday)
  • z – The day of the year (from 0 through 365)
  • W – The ISO-8601 week number of year (weeks starting on Monday)
  • F – A full textual representation of a month (January through December)
  • m – A numeric representation of a month (from 01 to 12)
  • M – A short textual representation of a month (three letters)
  • n – A numeric representation of a month, without leading zeros (1 to 12)
  • t – The number of days in the given month
  • L – Whether it’s a leap year (1 if it is a leap year, 0 otherwise)
  • o – The ISO-8601 year number
  • Y – A four digit representation of a year
  • y – A two digit representation of a year
  • a – Lowercase am or pm
  • A – Uppercase AM or PM
  • B – Swatch Internet time (000 to 999)
  • g – 12-hour format of an hour (1 to 12)
  • G – 24-hour format of an hour (0 to 23)
  • h – 12-hour format of an hour (01 to 12)
  • H – 24-hour format of an hour (00 to 23)
  • i – Minutes with leading zeros (00 to 59)
  • s – Seconds, with leading zeros (00 to 59)

More Details about date format, click here

admin

I am a well-organized professional in Drupal Development and PHP web development with strong script handling knowledge or automation process with PHP. I awarded 2 times in year by my company for the best employee of the quarter. I never work to complete my working hours. I worked for only achievement.