Jump to content


Photo

date format from MYSQL


  • Please log in to reply
5 replies to this topic

#1 ddevitt

ddevitt
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 07 March 2006 - 04:20 PM

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

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 07 March 2006 - 06:04 PM

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:
echo date('n/j/Y', strtotime($date));

good luck
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 XenoPhage

XenoPhage
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 07 March 2006 - 08:00 PM

Why not let MySQL do it for you?

SELECT DATE_FORMAT(date_field_name, '%m/%e/%Y') FROM table_name

--
[a href=\"http://blog.godshell.com\" target=\"_blank\"]XenoPhage[/a]
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Something mysterious is formed, born in the silent void. Waiting alone and unmoving, it is at once still and yet in constant motion. It is the source of all programs. I do not know its name, so I will call it the Tao of Programming.[/quote]

#4 ddevitt

ddevitt
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 07 March 2006 - 08:11 PM

thanks. I'll give that a shot.

#5 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 07 March 2006 - 08:21 PM

[!--quoteo(post=352592:date=Mar 7 2006, 03:00 PM:name=XenoPhage)--][div class=\'quotetop\']QUOTE(XenoPhage @ Mar 7 2006, 03:00 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Why not let MySQL do it for you?

SELECT DATE_FORMAT(date_field_name, '%m/%e/%Y') FROM table_name
[/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.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#6 ddevitt

ddevitt
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 07 March 2006 - 10:03 PM

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:

<?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)); ?>

I figured it out. I forgot the "AS" portion of the SQL statment.

Thanks to everyone who helped out!!!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users