Convert month short name to month

I have a variable with the following value

$month = 201002; 

the first 4 numbers represent the year, and the last 2 numbers represent the month. I need to get the last 2 numbers in the month string name, for example. February

My code is as follows

 <?php echo date('M',substr($month,4,6)); ?> 

I can get the name of the month

+7
php
source share
6 answers

Add "01", and strtotime will be able to parse the string:

 echo date('M', strtotime($month . '01')); 
+15
source share

The second date parameter is the timestamp. Use mktime to create it.

 $month = 201002; $monthNr = substr($month, -2, 2); $timestamp = mktime(0, 0, 0, $monthNr, 1); $monthName = date('M', $timestamp ); 
+7
source share

You can use the DateTime class to get the Date data structure using a date string and format. Then enter the date string in any format:

 $month = 201002; $date = DateTime::createFromFormat('Yd', $month); $monthName = $date->format('M'); // will get Month name 
+1
source share
 $mydate = "201002"; date('M', mktime(0, 0, 0, substr($mydate, 4, 2), 1, 2000)); 
0
source share

It can help you.

  $month = substr($month, -2, 2); echo date('M', strtotime(date('Y-'. $month .'-d'))); 
0
source share

As a programmer and not even knowing anything about PHP data magic, I did it

 $month = intval(substr($input_date,4,2)); $mons = explode(" ","Zer Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"); echo $mons[$month]; 
-one
source share

All Articles