ddevitt Posted March 7, 2006 Share Posted March 7, 2006 Hi everyone. I'm new here and to PHP. My problem is I'm pulling a date out of a MYSQL database and I want to format it like mm/dd/yyyy. Is there a PHP function that can do this? I'm aware of the date() function but I can't seem to make that work.Any help is greatly appreciated.ThanksDave Quote Link to comment Share on other sites More sharing options...
obsidian Posted March 7, 2006 Share Posted March 7, 2006 welcome to the forums! hope you find lots of help herea SQL date is formatted as "YYYY-MM-DD", and the date() function takes a UNIX timestamp, so you need to use the strtotime() function first, and then the date() function. try something like this:[code]echo date('n/j/Y', strtotime($date));[/code]good luck Quote Link to comment Share on other sites More sharing options...
XenoPhage Posted March 7, 2006 Share Posted March 7, 2006 Why not let MySQL do it for you?[code]SELECT DATE_FORMAT(date_field_name, '%m/%e/%Y') FROM table_name[/code] Quote Link to comment Share on other sites More sharing options...
ddevitt Posted March 7, 2006 Author Share Posted March 7, 2006 thanks. I'll give that a shot. Quote Link to comment Share on other sites More sharing options...
obsidian Posted March 7, 2006 Share Posted March 7, 2006 [!--quoteo(post=352592:date=Mar 7 2006, 03:00 PM:name=XenoPhage)--][div class=\'quotetop\']QUOTE(XenoPhage @ Mar 7 2006, 03:00 PM) [snapback]352592[/snapback][/div][div class=\'quotemain\'][!--quotec--]Why not let MySQL do it for you?[code]SELECT DATE_FORMAT(date_field_name, '%m/%e/%Y') FROM table_name[/code][/quote]good suggestion, but it goes back to a discussion i had with someone yesterday: if you're going to just use a fixed date on the page, and you only need to format it once into a variable for display, you're better off using XenoPhage's suggestion. however, if you're pulling a datetime and then using parts of it or formatting it differently throughout the page, you'll be better off pulling it once and letting PHP do the formatting for you instead of hitting the database every time. Quote Link to comment Share on other sites More sharing options...
ddevitt Posted March 7, 2006 Author Share Posted March 7, 2006 Thanks for all of the help so far. I attempted to alter the query and let MYSQL handle it, but I am getting the following error. Notice: Undefined index: mdate in c:\Inetpub\wwwroot\dave\PHPDate.php on line 20here is my code:[code]<?phpmysql_select_db($database_daveConn, $daveConn);$query_Recordset1 = "SELECT title, DATE_FORMAT(mdate, '%m/%e/%Y') FROM news";$Recordset1 = mysql_query($query_Recordset1, $daveConn) or die(mysql_error());$row_Recordset1 = mysql_fetch_assoc($Recordset1);$totalRows_Recordset1 = mysql_num_rows($Recordset1);?><?php do { ?> <tr> <td><?php echo $row_Recordset1['mdate']; ?></td> <td><?php echo $row_Recordset1['title']; ?></td> </tr> <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>[/code]I figured it out. I forgot the "AS" portion of the SQL statment.Thanks to everyone who helped out!!! 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.