Jump to content

date format from MYSQL


ddevitt

Recommended Posts

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.

Thanks
Dave
Link to comment
Share on other sites

welcome to the forums! hope you find lots of help here

a 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
Link to comment
Share on other sites

[!--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.
Link to comment
Share on other sites

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 20

here is my code:

[code]<?php
mysql_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!!!
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.