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
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

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.