SonicGuy756 Posted November 30, 2005 Share Posted November 30, 2005 I already have a script set up to pull data from my database. I have a DATE row. I want to be able to change the format of the displayed date on the page from something such as "2004-01-06" to "Jan 06, 2005". I've searched everywhere and still can't seem to find out exactly how to do this. Can anyone help me out here? Here's the code I'm using: $dbcnx = @mysql_connect("localhost", "sonicpl_sncpla", "*******"); if (!$dbcnx) { echo( "Unable to connect to the " . "database server at this time." ); exit(); } if (! @mysql_select_db("sonicpl_sncpla") ) { echo( "Unable to select database!" ); exit(); } $result = mysql_query( "SELECT * FROM site_games"); if (!$result) { echo("<P>Error performing query: " . mysql_error() . "</P>"); exit(); } // Display the text of each joke in a paragraph while ( $row = mysql_fetch_array($result) ) { echo("<a href=\"../games.php?id=".$row["id"]."\"><b>".$row["title"]."</b></a> - ".$row["usa_release"]."<br />".$row["intro"]."<br /><br />"); } Link to comment https://forums.phpfreaks.com/topic/2961-convert-date-format/ Share on other sites More sharing options...
ryanlwh Posted November 30, 2005 Share Posted November 30, 2005 i thought you've asked it in one of the php forums already Link to comment https://forums.phpfreaks.com/topic/2961-convert-date-format/#findComment-9966 Share on other sites More sharing options...
tucker Posted December 7, 2005 Share Posted December 7, 2005 I researched a little and found a way to do it. You'll use two functions: date and mktime. Date returns the date based on given variables. Your given variables will be the date you want converted. Look at this code: echo date("M-d-Y", mktime(0, 0, 0, 1, 6, 2005)); The first part of the date function, "M-d-Y" displays your date in a specific format ([a href=\"http://www.php.net/manual/en/function.date.php\" target=\"_blank\"]see this page for different formats[/a]). The mktime converts 06 (the sixth) of 01 (january) of 2005 (year). Simply have your date put in those spaces to be formatted, like so: echo date("M-d-Y", mktime(0, 0, 0, $yourmonth, $yourday, $youryear)); Hope this helps. -Seth Link to comment https://forums.phpfreaks.com/topic/2961-convert-date-format/#findComment-10072 Share on other sites More sharing options...
Arenium Posted December 11, 2005 Share Posted December 11, 2005 I don't mean to one-up anybody, but the general method for accomplishing this is through the use of the two functions date and strtotime: $date = "2004-06-23"; // Whatever you pull from your database echo date('M d, Y', strtotime($date)); // Should output "Jun 23, 2004" Link to comment https://forums.phpfreaks.com/topic/2961-convert-date-format/#findComment-10126 Share on other sites More sharing options...
ryanlwh Posted December 12, 2005 Share Posted December 12, 2005 an alternative way is to use DATE_FORMAT function in mysql [!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] DATE_FORMAT(dateField,'%b %e, %Y') FROM table1 [!--sql2--][/div][!--sql3--] Link to comment https://forums.phpfreaks.com/topic/2961-convert-date-format/#findComment-10146 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.