Jump to content

Converting number to month string


outside5

Recommended Posts

Hi,

 

I have recently started learning PHP coming from a ColdFusion background.  IN CF there is a function to return the month as a string from a given numerical value - e.g. 01 = January and 09 = September.

 

Does PHP have a similar way to return the month from a given number?

 

TIA,

Paul

Link to comment
https://forums.phpfreaks.com/topic/94764-converting-number-to-month-string/
Share on other sites

You would have to create your own function.

<?php
function getmonth($n){
$time = mktime(0, 0, 0, $n, 1, 2008);
return date("F", $time);
}
echo getmonth("4");  // Will output April
?>

 

now you can just use getmonth("monthnumber") to get the month name

 

if you create some kind of file like functions.php, you can put all your custom functions in it. Then at the top of your pages just use

include('functions.php');

 

And all the functions in the functions file will be available on that page

 

Ray

 

Thanks Craygo,

 

So if I were looking to dynamically create a drop down list of months, hwo would I need to amend the following?

 

<select name="month" class="dateField">

<option value="">MM</option>

<?php

function getmonth($n){

$time = mktime(0, 0, 0, $n, 1, 2008);

return date("F", $time);

}

 

 

for ($month = 1; $month <= 12; $month++) {

echo '<option value="' . $month . '">' . getmonth($month) . '</option>';

?>

</select>

look ok. The function can go anywhere on your page. You don't have to clutter up the select portion with the function.

 

<?php
function getmonth($n){
$time = mktime(0, 0, 0, $n, 1, 2008);
return date("F", $time);
}
?>
<select name="month" class="dateField">
  <option value="">MM</option>
<?php
  for ($month = 1; $month <= 12; $month++) {
  echo '<option value="' . $month . '">' . getmonth($month) . '</option>';
  }
?>
</select>

 

Ray

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.