outside5 Posted March 6, 2008 Share Posted March 6, 2008 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 Quote Link to comment Share on other sites More sharing options...
craygo Posted March 6, 2008 Share Posted March 6, 2008 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 Quote Link to comment Share on other sites More sharing options...
outside5 Posted March 6, 2008 Author Share Posted March 6, 2008 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> Quote Link to comment Share on other sites More sharing options...
craygo Posted March 6, 2008 Share Posted March 6, 2008 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 Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 6, 2008 Share Posted March 6, 2008 I usually use the strtotime() function instead of mktime() <?php function getmonth($n) { return(date'F',strtotime('2008-' . $n . '-01')); } ?> Ken Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.