twilitegxa Posted July 21, 2009 Share Posted July 21, 2009 How do you write the php script that will display the field birth_month from my table as the name of the month rather than the number of the month? I have it currently saved into the database as the number of the month, but I would like to display it as the name of the month. How do I do this? Link to comment https://forums.phpfreaks.com/topic/166879-solved-setting-display-of-months/ Share on other sites More sharing options...
ldougherty Posted July 22, 2009 Share Posted July 22, 2009 I'm sure you could do it using the date() and mktime() functions but the easiest solution would be to create an array. $month = array('Jan','Feb','etc'); Then reference the month using the number from your database ie $month[1] would be Feb Link to comment https://forums.phpfreaks.com/topic/166879-solved-setting-display-of-months/#findComment-879912 Share on other sites More sharing options...
TomNomNom Posted July 22, 2009 Share Posted July 22, 2009 While making an array is one option, you have the problem that the indexes are one adrift (I.E. February isn't the first Month). Granted, that's not a difficult problem to solve, but using date() an mktime() makes more sense IMO :-) <?php $month = 3; echo date('F', mktime(0, 0, 0, $month)); //March You could even wrap it in a function <?php function getMonthName($month_number){ return date('F', mktime(0, 0, 0, $month_number)); } echo getMonthName(5); //May Link to comment https://forums.phpfreaks.com/topic/166879-solved-setting-display-of-months/#findComment-879919 Share on other sites More sharing options...
ldougherty Posted July 22, 2009 Share Posted July 22, 2009 Yea, that is a better idea. I knew it could be done but couldn't put the logic together quickly Link to comment https://forums.phpfreaks.com/topic/166879-solved-setting-display-of-months/#findComment-879920 Share on other sites More sharing options...
twilitegxa Posted July 22, 2009 Author Share Posted July 22, 2009 Here is my current code for a profile for a character: <?php session_start(); //Access Tracking Snippet //set up static variables $page_title = "showprofile.php"; $user_agent = getenv("HTTP_USER_AGENT"); $date_accessed = date("Y-m-d"); //connect to server and select database $conn = mysql_connect("localhost", "root", "") or die(mysql_error()); $db = mysql_select_db("smrpg", $conn) or die(mysql_error()); //create and issue query $sql = "insert into access_tracker values ('', '$page_title', '$user_agent', '$date_accessed')"; mysql_query($sql,$conn); ?> <?php //check for required info from the query string if (!$_GET['id']) { header("Location: listcharacters.php"); exit; } //connect to server and select database $conn = mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("smrpg", $conn) or die(mysql_error()); //verify the topic exists $verify_topic = "select identity from scouts where id = $_GET[id]"; $verify_topic_res = mysql_query($verify_topic, $conn) or die(mysql_error()); if (mysql_num_rows($verify_topic_res) < 1) { //this character does not exist $display_block = "<p><em>You have selected an invalid character. Please <a href=\"listcharacters.php\">try again</a></em></p>"; } else { //gather rest of profile $get_posts = "select *, date_format(create_time, '%b %e %Y at %r') as fmt_scout_create_time from scouts where id = $_GET[id]"; $get_posts_res = mysql_query($get_posts, $conn) or die(mysql_error()); //create the display string $display_block = " <table cellpadding=3 cellspacing=1 border=1>"; while ($posts_info = mysql_fetch_array($get_posts_res)) { $post_id = $posts_info['id']; $identity = nl2br(stripslashes($posts_info['identity'])); $character_create_time = $posts_info['fmt_scout_create_time']; $username = $posts_info['username']; $biography = nl2br(stripslashes($posts_info['biography'])); $name = $posts_info['name']; $element_of_influence = $posts_info['element_of_influence']; $age = $posts_info['age']; $birth_month = $posts_info['birth_month']; $birth_date = $posts_info['birth_date']; $birth_year = $posts_info['birth_year']; $height_feet = $posts_info['height_feet']; $height_inches = $posts_info['height_inches']; $blood_type = $posts_info['blood_type']; $hobbies = $posts_info['hobbies']; $favorite_color = $posts_info['favorite_color']; $favorite_gemstone = $posts_info['favorite_gemstone']; $favorite_food = $posts_info['favorite_food']; $least_favorite_food = $posts_info['least_favorite_food']; $favorite_school_subject = $posts_info['favorite_school_subject']; $least_favorite_school_subject = $posts_info['least_favorite_school_subject']; $strengths = $posts_info['strengths']; $weaknesses = $posts_info['weaknesses']; $goal = $posts_info['goal']; $mission = $posts_info['mission']; $biography = $posts_info['biography']; //add to display $display_block .= " <tr> <td width=24% valign=top><strong>Character Name:</strong></td> <td width=55% valign=top>$name</td> <td align=center valign=top rowspan=18><img src=\"image.gif\" height=\"500\" width=\"200\"></td> </tr> <tr> <td><strong>Element Of Influence:</strong></td> <td>$element_of_influence</td> </tr> <tr> <td><strong>Age:</strong></td> <td>$age</td> </tr> <tr> <td><strong>Date Of Birth:</strong></td> <td>$birth_month-$birth_date-$birth_year</td> </tr> <tr> <td><strong>Height:</strong></td> <td>$height_feet feet $height_inches inches</td> </tr> <tr> <td><strong>Blood Type:</strong></td> <td>$blood_type</td> </tr> <tr> <td><strong>Hobbies:</strong></td> <td>$hobbies</td> </tr><tr> <td><strong>Favorite Color:</strong></td> <td>$favorite_color</td> </tr> <tr> <td><strong>Favorite Gemstone:</strong></td> <td>$favorite_gemstone</td> </tr> <tr> <td><strong>Favorite Food:</strong></td> <td>$favorite_food</td> </tr> <tr> <td><strong>Least Favorite Food:</strong></td> <td>$least_favorite_food</td> </tr> <tr> <td><strong>Favorite School Subject:</strong></td> <td>$favorite_school_subject</td> </tr> <tr> <td><strong>Least Favorite School Subject:</strong></td> <td>$least_favorite_school_subject</td> </tr> <tr> <td><strong>Strengths:</strong></td> <td>$strengths</td> </tr> <tr> <td><strong>Weaknesses:</strong></td> <td>$weaknesses</td> </tr> <tr> <td><strong>Goal:</strong></td> <td>$goal</td> </tr> <tr> <td><strong>Mission:</strong></td> <td>$mission</td> </tr> <td><strong>Character Biography:</strong></td> <td>$biography</td> </tr> <tr> <td> </td> <td> </td> <td valign=left>Created By: $username<br> Created On: [$character_create_time]</td> </tr>"; } //close up the table $display_block .= "</table>"; } ?> <html> <head> <title><?php print $identity; ?>'s Profile</title> <style type="text/css" media="screen"> /*<![CDATA[*/ @import url(global.css); /*]]>*/ </style> </head> <body> <!-- HEADER --> <h1 class="logo">Sailor Moon RPG</h1> <!-- /HEADER --> <?php include("topnav.php"); ?> <div id="main"> <?php include("includes/log.php"); ?> <?php include("mainnav.php"); ?> <h1 align="center"><?php print $identity; ?></h1> <?php print $display_block; ?> </div> <?php include("bottomnav.php"); ?><!-- FOOTER --> <!-- FOOTER --> <div id="footer_wrapper"> <div id="footer"> <p>Sailor Moon and all characters are<br /> trademarks of Naoko Takeuchi.</p> <p>Copyright © 2009 Liz Kula. All rights reserved.<br /> A product of <a href="#" target="_blank">Web Designs By Liz</a> systems.</p> <div id="foot-nav"> <ul> <li><a href="http://validator.w3.org/check?uri=http://webdesignsbyliz.com/digital/index.php" target="_blank"><img src="http://www.w3.org/Icons/valid-xhtml10-blue" alt="Valid XHTML 1.0 Transitional" height="31" width="88" /></a></li> <li><a href="http://jigsaw.w3.org/css-validator/validator?uri=http://webdesignsbyliz.com/digital/global.css" target="_blank"><img class="c2" src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS!" /></a></li> </ul> </div> </div> </div> <!-- /FOOTER --> </body> </html> How could I implement that function you suggested? I'm not too god with functions yet. I want the birth_month field to be displayed as the name of the month. Link to comment https://forums.phpfreaks.com/topic/166879-solved-setting-display-of-months/#findComment-879939 Share on other sites More sharing options...
twilitegxa Posted July 22, 2009 Author Share Posted July 22, 2009 Here is where the $birth_month variable gets named: $birth_month = $posts_info['birth_month']; So how can I use this function to display the month name? Link to comment https://forums.phpfreaks.com/topic/166879-solved-setting-display-of-months/#findComment-880006 Share on other sites More sharing options...
haku Posted July 22, 2009 Share Posted July 22, 2009 echo date('F', mktime(0, 0, 0, $birth_month)); Link to comment https://forums.phpfreaks.com/topic/166879-solved-setting-display-of-months/#findComment-880011 Share on other sites More sharing options...
twilitegxa Posted July 22, 2009 Author Share Posted July 22, 2009 It isn't working because of how i have the php I guess: $display_block .= " <tr> <td width=24% valign=top><strong>Character Name:</strong></td> <td width=55% valign=top>$name</td> <td align=center valign=top rowspan=18><img src=\"image.gif\" height=\"500\" width=\"200\"></td> </tr> <tr> <td><strong>Element Of Influence:</strong></td> <td>$element_of_influence</td> </tr> <tr> <td><strong>Age:</strong></td> <td>$age</td> </tr> <tr> <td><strong>Date Of Birth:</strong></td> <td>$birth_month-$birth_date-$birth_year</td> </tr> ... more ... Link to comment https://forums.phpfreaks.com/topic/166879-solved-setting-display-of-months/#findComment-880018 Share on other sites More sharing options...
twilitegxa Posted July 22, 2009 Author Share Posted July 22, 2009 I got it. I just made it into a variable and used it that way: $getMonth = date('F', mktime(0, 0, 0, $birth_month)); Thanks for the help everyone! Link to comment https://forums.phpfreaks.com/topic/166879-solved-setting-display-of-months/#findComment-880021 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.