There is a way to get the name of the month by setting an integer value of PHP

Hi, I am using PHP. I want to pass an integer value (1-12) and get the corresponding month name, is there a way in PHP to do this, or should I do it myself by initializing an array of month names.

I want to do as

$month_name = get_month_name(1); echo $month_name ; //echo january 

early

+6
source share
7 answers
 echo date('F', strtotime("2012-$int-01")); 
+20
source

Another inline way is

 $monthInt = 3; $monthName = DateTime::createFromFormat('m', $monthInt)->format('F'); 

It would be nice if PHP had a built-in way to get date names without creating a date object.

+5
source

Use this code to get the month name by specifying an integer value of PHP

  <?php function get_month_name($inp) { return date("F", strtotime(date("d-$inp-y"))); } $month_name = get_month_name("1"); echo $month_name; ?> 
+4
source
 function get_month_name($month) { $months = array( 1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April', 5 => 'May', 6 => 'June', 7 => 'July', 8 => 'August', 9 => 'September', 10 => 'October', 11 => 'November', 12 => 'December' ) return $months[$month]; } 
+2
source
 $month = 1; //month in numeric echo date('F', mktime(0, 0, 0, $month, 10)); //print January 
+1
source

You can build Unix time (in 1970) by multiplying the month number by the average number of seconds per month (2628000) and taking 15 days so that it is always somewhere in the middle of the month. Thus, it works well and avoids unnecessary overhead:

 function month_name($i) { return date('F', $i * 2628000 - 1314000); } 

Actual dates (if you replace 'd FY' with 'F') change from January 16, 1970 to December 16, 1970 with $ i from 1 to 12. You can do a similar trick to get the names of the days (from 1 - 7 - Monday Sunday):

 function day_name($i) { return date('l', $i * 86400 + 302400); } 
0
source

use the mktime () function, which accepts date elements as parameters.

 <?php $month_number= 3; $month_name = date("F", mktime(0, 0, 0, $month_number, 10)); echo $month_name; ?> 

Conclusion: March

In the mktime () function, the parameters are hour, minute, second, month, day, year.

For more information check http://php.net/manual/en/function.mktime.php

0
source

Source: https://habr.com/ru/post/910769/


All Articles